console
function loadMap (key, plugins, v = '1.4.14') {
return new Promise(function (resolve, reject) {
if (typeof AMap !== 'undefined') {
resolve(AMap)
return true
}
window.onCallback = function () {
resolve(AMap)
}
let script = document.createElement('script')
script.type = 'text/javascript'
script.src = `https:
script.onerror = reject
document.head.appendChild(script)
})
}
var Main = {
data () {
return {
GDMap: null,
plugins: [
'AMap.OverView',
'AMap.MouseTool',
'AMap.PolyEditor',
'AMap.RectangleEditor',
'AMap.PlaceSearch',
'AMap.DistrictLayer',
'AMap.CustomLayer'
],
key: 'c5eac55551560531336988396dacbf53',
v: '1.4.14',
loading: true,
polygons: [
[
[ 116.402921, 39.984507 ],
[ 116.531324, 39.938719 ],
[ 116.525144, 39.858648 ],
[ 116.423521, 39.821742 ]
]
],
polygonsGroup: null,
isDrawing: false,
drawingPolygon: {
polyline: null,
polylinePath: [],
polygon: null,
pointsGroup: null,
pointOnLine: null,
isOnPolygon: null
},
styles: {
circleMarker: {
radius: 4,
strokeColor: '#010301',
strokeWeight: 2,
strokeOpacity: 1,
fillColor: '#FFFFFF',
fillOpacity: 1,
bubble: true,
cursor: 'pointer',
clickable: true,
zIndex: 999999
},
drawingPolyline: {
strokeColor: '#dd9ab0',
strokeWeight: 5,
strokeStyle: 'dashed',
strokeDasharray: [5, 20],
bubble: true
},
polygon: {
fillColor: '#DC3021',
fillOpacity: 0.2,
strokeColor: '#DC3021',
strokeWeight: 1,
strokeOpacity: 0.9
}
}
}
},
mounted () {
loadMap(this.key, this.plugins, this.v)
.then(AMap => {
this.GDMap = new AMap.Map('GDMap', {
zoom: 11,
center: [116.397428, 39.90923],
isHotspot: false
})
this.GDMap.on('click', this.mapOnClick)
this.GDMap.on('dblclick', this.mapOnDblclick)
this.GDMap.on('mousemove', this.mapOnMouseMove)
this.GDMap.on('complete', () => {
this.polygons.forEach(polygon => {
this.addPolygon(polygon)
})
})
})
.catch(() => {
console.log('地图加载失败!')
})
},
methods: {
drawPolygon () {
this.isDrawing = true
},
mapOnClick (ev) {
if (!this.isDrawing) return
let position = [ev.lnglat.lng, ev.lnglat.lat]
if (this.drawingPolygon.isOnPolygon && this.drawingPolygon.pointOnLine) {
const center = this.drawingPolygon.pointOnLine.getCenter()
position = [center.lng, center.lat]
}
this.addPolygonPoint(position)
this.addDrawingPolyline([position, position])
this.drawPolygonByPoints()
},
mapOnDblclick (ev) {
if (!this.isDrawing) return
this.complateDraw()
},
mapOnMouseMove (ev) {
if (!this.isDrawing) return
const position = [ev.lnglat.lng, ev.lnglat.lat]
const linePath = this.getPointLine(position)
let recentPoint = null
if (this.drawingPolygon.isOnPolygon) {
recentPoint = this.getRecentPoint(linePath, position)
this.addDrawingOnLinePoint(recentPoint)
} else {
this.removeDrawingOnLinePoint()
}
this.setDrawingPolyline(position)
},
addPolygonPoint (position) {
const option = {
...this.styles.circleMarker,
center: position
}
const circlePointMarker = new AMap.CircleMarker(option)
if (!this.drawingPolygon.pointsGroup) {
this.drawingPolygon.pointsGroup = new AMap.OverlayGroup()
this.GDMap.add(this.drawingPolygon.pointsGroup)
}
this.drawingPolygon.pointsGroup.addOverlay(circlePointMarker)
},
addDrawingPolyline (paths) {
if (this.drawingPolygon.polyline) {
this.drawingPolygon.polyline.setPath(paths)
} else {
const option = {
...this.styles.drawingPolyline,
path: paths
}
this.drawingPolygon.polyline = new AMap.Polyline(option)
this.GDMap.add(this.drawingPolygon.polyline)
}
this.drawingPolygon.polylinePath = paths
},
setDrawingPolyline (position) {
if (this.drawingPolygon.polyline) {
const linePath = [
this.drawingPolygon.polylinePath[0],
position
]
this.drawingPolygon.polyline.setPath(linePath)
this.drawingPolygon.polylinePath = linePath
}
},
drawPolygonByPoints () {
const pointsGroup = this.drawingPolygon.pointsGroup
const pointsLength = pointsGroup ? pointsGroup.getOverlays().length : 0
if (pointsLength > 1) {
const paths = pointsGroup.getOverlays().map(item => {
const path = item.getCenter()
return [path.lng, path.lat]
})
if (this.drawingPolygon.polygon) {
this.drawingPolygon.polygon.setPath(paths)
} else {
const option = {
...this.styles.polygon,
path: paths
}
this.drawingPolygon.polygon = new AMap.Polygon(option)
this.drawingPolygon.polygon.setMap(this.GDMap)
}
}
},
complateDraw () {
const paths = this.drawingPolygon.polygon.getPath().map(item => [item.lng, item.lat])
this.isDrawing = false
this.polygons.push(paths)
this.addPolygon(paths)
this.clearUselessOverlays()
},
clearUselessOverlays () {
if (!this.GDMap) return
this.GDMap.remove(this.drawingPolygon.polyline)
this.GDMap.remove(this.drawingPolygon.polygon)
this.GDMap.remove(this.drawingPolygon.pointsGroup)
if (this.drawingPolygon.pointOnLine) {
this.GDMap.remove(this.drawingPolygon.pointOnLine)
}
this.drawingPolygon.polyline = null
this.drawingPolygon.polygon = null
this.drawingPolygon.pointsGroup = null
this.drawingPolygon.polylinePath = []
this.drawingPolygon.isOnPolygon = false
},
addPolygon (paths) {
if (!this.GDMap) return
const option = {
...this.styles.polygon,
path: [...paths]
}
const polygon = new AMap.Polygon(option)
if (!this.polygonsGroup) {
this.polygonsGroup = new AMap.OverlayGroup()
this.GDMap.add(this.polygonsGroup)
}
this.polygonsGroup.addOverlay(polygon)
},
addDrawingOnLinePoint (center) {
if (this.drawingPolygon.pointOnLine) {
this.drawingPolygon.pointOnLine.setCenter(center)
return
}
const option = {
map: this.GDMap,
center: center,
...this.styles.circleMarker
}
this.drawingPolygon.pointOnLine = new AMap.CircleMarker(option)
},
removeDrawingOnLinePoint () {
if (this.drawingPolygon.pointOnLine) {
this.GDMap.remove(this.drawingPolygon.pointOnLine)
this.drawingPolygon.pointOnLine = null
}
},
getPointLine (position) {
const resolution = this.GDMap.getResolution()
const pointWidth = 6 * resolution
let linePath = []
this.drawingPolygon.isOnPolygon = false
for (let i = 0; i < this.polygons.length; i++) {
const itemPath = this.polygons[i]
let hasFind = false
for (let n = 0; n < itemPath.length; n++) {
const path = itemPath[n]
const nextPath = itemPath[n + 1]
const line = nextPath ? [path, nextPath] : [path, itemPath[0]]
const isPointOnSegment = AMap.GeometryUtil.isPointOnSegment(position, line[0], line[1], pointWidth)
if (isPointOnSegment) {
linePath = line
this.drawingPolygon.isOnPolygon = true
hasFind = true
break
}
}
if (hasFind) break
}
return linePath
},
getRecentPoint (paths, curPointPosition) {
const recentPoint = AMap.GeometryUtil.closestOnLine(curPointPosition, paths)
return recentPoint
}
},
watch: {
isDrawing (newState) {
if (newState) {
this.GDMap.setDefaultCursor('crosshair')
} else {
this.GDMap.setDefaultCursor('')
}
this.GDMap.setStatus({
doubleClickZoom: !newState
})
}
}
}
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')
<script src="https://unpkg.com/vue@2.6.10/dist/vue.js">
</script>
<div id="app">
<template>
<div class="map">
<button type="primary" @click="drawPolygon">
画多边形
</button>
<div id="GDMap">
</div>
</div>
</template>
</div>
#GDMap {
width: 1200px;
height: 500px;
position: relative;
}