vue 百度地图 自定义覆盖物!
<template>
<div id="app">
<div class="Overlay" style="margin:10px 0;">
<el-button @click="deletePoint">删除点el-button>
<el-button @click="deleteCircle">删除圆el-button>
<el-button @click="deletePolyline">删除线el-button>
<el-button @click="deletePolygon">删除多边形el-button>
<el-button @click="deleteRectangle">删除矩形el-button>
<el-button @click="deleteAll">删除全部el-button>
div>
<div id="allmap" ref="allmap" />
div>
template>
<script>
export default {
name: 'App',
components: {
},
data(){
return {
map: {
},
circle: null,
circleCenter: [],
circleRadius: '',
circleArea: '',
polygonArea: '',
pointArr: [],
circleArr: [],
polylineArr: [],
polygonArr: [],
rectangleArr: []
}
},
mounted() {
this.initmap()
this.initdrawingManager()
},
methods(){
initmap() {
this.map = new BMap.Map('allmap')
const point = new BMap.Point(116.404, 39.915)
this.map.centerAndZoom(point, 15)
this.map.setCurrentCity('北京')
this.map.enableScrollWheelZoom(true)
this.map.addControl(new BMap.NavigationControl())
this.map.addControl(new BMap.OverviewMapControl({
isOpen: true, anchor: BMAP_ANCHOR_BOTTOM_RIGHT }))
this.map.enableInertialDragging()
this.map.enableContinuousZoom()
},
initdrawingManager() {
this.deleteAll()
var styleOptions = {
strokeColor: 'blue',
fillColor: 'skyblue',
strokeWeight: 3,
strokeOpacity: 0.5,
fillOpacity: 0.4,
strokeStyle: 'solid'
}
this.drawingManager = new BMapLib.DrawingManager(this.map, {
isOpen: false,
enableDrawingTool: true,
drawingToolOptions: {
anchor: BMAP_ANCHOR_TOP_RIGHT,
offset: new BMap.Size(5, 5)
},
circleOptions: styleOptions,
polylineOptions: styleOptions,
polygonOptions: styleOptions,
rectangleOptions: styleOptions
})
this.drawingManager.addEventListener('overlaycomplete', this.overlaycomplete.bind(this))
},
overlaycomplete(e) {
if (e.drawingMode === 'circle') {
this.clearData()
this.circleArr.push(e.overlay)
var removeCircle = (e, ee, circle) => {
this.map.removeOverlay(circle)
}
var circleMenu = new BMap.ContextMenu()
circleMenu.addItem(new BMap.MenuItem('删除圆', removeCircle.bind(e.overlay)))
e.overlay.addContextMenu(circleMenu)
} else if (e.drawingMode === 'marker') {
this.clearData()
this.pointArr.push(e.overlay)
var removeMarker = (e, ee, marker) => {
this.map.removeOverlay(marker)
}
var markerMenu = new BMap.ContextMenu()
markerMenu.addItem(new BMap.MenuItem('删除点', removeMarker.bind(e.overlay)))
e.overlay.addContextMenu(markerMenu)
} else if (e.drawingMode === 'polyline') {
this.clearData()
this.polylineArr.push(e.overlay)
var removePolyline = (e, ee, circle) => {
this.map.removeOverlay(circle)
}
var polylineMenu = new BMap.ContextMenu()
polylineMenu.addItem(new BMap.MenuItem('删除线', removePolyline.bind(e.overlay)))
e.overlay.addContextMenu(polylineMenu)
} else if (e.drawingMode === 'polygon') {
this.clearData()
this.polygonArr.push(e.overlay)
var removePolygon = (e, ee, circle) => {
this.map.removeOverlay(circle)
}
var polygonMenu = new BMap.ContextMenu()
polygonMenu.addItem(new BMap.MenuItem('删除多边形', removePolygon.bind(e.overlay)))
e.overlay.addContextMenu(polygonMenu)
} else {
this.clearData()
this.rectangleArr.push(e.overlay)
var removeRectangle = (e, ee, circle) => {
this.map.removeOverlay(circle)
}
var rectangleMenu = new BMap.ContextMenu()
rectangleMenu.addItem(new BMap.MenuItem('删除矩形', removeRectangle.bind(e.overlay)))
e.overlay.addContextMenu(rectangleMenu)
}
},
clearData() {
this.circleRadius = ''
this.circleArea = ''
this.polygonArea = ''
},
deleteCircle() {
this.circleArr.forEach(e => {
this.map.removeOverlay(e)
})
this.circleArr = []
},
deletePoint() {
this.pointArr.forEach(e => {
this.map.removeOverlay(e)
})
this.pointArr = []
},
deletePolyline() {
this.polylineArr.forEach(e => {
this.map.removeOverlay(e)
})
this.polylineArr = []
},
deletePolygon() {
this.polygonArr.forEach(e => {
this.map.removeOverlay(e)
})
this.polygonArr = []
},
deleteRectangle() {
this.rectangleArr.forEach(e => {
this.map.removeOverlay(e)
})
this.rectangleArr = []
},
deleteAll() {
this.clearData()
this.map.clearOverlays()
}
}
}
</script>