vue中实现supermap标绘
Vue 中实现 SuperMap 标绘
在 Vue 项目中集成 SuperMap 的标绘功能,需要引入 SuperMap 的 JavaScript API,并结合 Vue 的生命周期和组件化特性实现地图标绘功能。以下是具体实现步骤:
引入 SuperMap API
在项目的 index.html 中引入 SuperMap 的 JavaScript API:
<script src="https://iclient.supermap.io/web/libs/iclient8c/iclient8c.min.js"></script>
或通过 npm 安装:
npm install @supermap/iclient-js
在 Vue 组件中动态加载:
import '@supermap/iclient-js';
初始化地图
在 Vue 组件的 mounted 钩子中初始化 SuperMap 地图:
mounted() {
this.initMap();
},
methods: {
initMap() {
const url = "https://iserver.supermap.io/iserver/services/map-world/rest/maps/World";
this.map = new SuperMap.Map("map", {
controls: [new SuperMap.Control.Navigation(), new SuperMap.Control.Zoom()],
allOverlays: true
});
this.map.addLayer(new SuperMap.Layer.TiledDynamicRESTLayer("World", url, null, { maxResolution: "auto" }));
this.map.setCenter(new SuperMap.LonLat(0, 0), 2);
}
}
模板中需预留地图容器:
<div id="map" style="width: 100%; height: 500px;"></div>
实现标绘功能
通过 SuperMap 的 DrawFeature 控件实现标绘:
-
添加标绘控件
addDrawTool() { const layer = new SuperMap.Layer.Vector("Vector Layer"); this.map.addLayer(layer); this.drawControl = new SuperMap.Control.DrawFeature( layer, SuperMap.Handler.Polygon, { handlerOptions: { multi: true } } ); this.map.addControl(this.drawControl); } -
触发标绘
startDrawing(type) { this.drawControl.handler.stop(); this.drawControl.handler = new SuperMap.Handler[type](this.drawControl.layer, { multi: true }); this.drawControl.activate(); } -
清除标绘
clearDrawings() { this.drawControl.layer.destroyFeatures(); }
事件监听
监听标绘完成事件以获取标绘图形数据:

this.drawControl.layer.events.on({
"featuresadded": (event) => {
const feature = event.features[0];
console.log(feature.geometry.getBounds().toArray());
}
});
注意事项
- 跨域问题:若使用本地 iServer 服务,需确保服务端允许跨域。
- 资源释放:在组件销毁时移除地图和控件:
beforeDestroy() { if (this.map) this.map.destroy(); } - 动态加载:若通过 CDN 引入 SuperMap API,需确保其在 Vue 实例化前加载完成。
通过以上步骤,可以在 Vue 中实现 SuperMap 的基础标绘功能,包括点、线、面的绘制和交互。






