initOpenlayer(features: Feature[]) {
// 设置地图的显示区域,使用图片的宽高作为地图的范围
this.extent = [0, 0, this.imageWidth, this.imageHeight]
// 创建自定义的投影对象,这里使用了像素单位来处理地图坐标
this.projection = new Projection({
code: 'xkcd-image', // 自定义投影的代码
units: 'pixels', // 使用像素单位
extent: this.extent, // 定义地图的有效范围(这里是图片的宽高)
// 创建视图对象,用于设置地图的视角、缩放等属性
projection: this.projection, // 使用上面创建的投影
center: getCenter(this.extent), // 计算地图中心点
// 创建地图数据源,存放要展示的地理数据(如绘制的矢量图形)
this.source = new VectorSource({
features: features, // 传入的特征(features)数组,即地图上要显示的对象
// 创建矢量图层,将数据源(source)与图层(vector)关联
this.vector = new VectorLayer({
source: this.source, // 使用之前创建的数据源
// 获取地图容器的DOM元素(`map`),用于初始化地图
let mapContainer = this.$refs.map as HTMLElement
target: mapContainer, // 将地图绑定到指定的容器(mapContainer)
interactions: defaultInteractions({
doubleClickZoom: false, // 禁用双击缩放功能
attributions: '', // 版权信息,通常为空
url: '', // 图片的URL(可以设置为背景图片路径)
projection: this.projection, // 使用自定义投影
imageExtent: this.extent, // 图片的显示范围,与地图范围一致
view: this.view, // 设置地图的视图(显示范围、缩放等)
// 添加多选交互功能,用于选择地图上的多个要素
this.addMultipleSelectInteraction()