使用AMap.Polygon和AMap.PolyEditor插件绘制和编辑多边形
使用MouseTool插件可以在地图上绘制Marker、Polyline、Polygon和Circle
官方文档
MouseTool结合AMap.PolyEditor实现动态画区域及区域编辑
<script src="https://webapi.amap.com/maps?
v=1.4.15&key=key&plugin=AMap.MouseTool,AMap.PolyEditor"></script>
<el-form-item label="添加区域" prop="sceneRange">
<el-button :disabled="noClick" @click="addArea" size="mini" v-model="addForm.sceneRange" >添加区域</el-button>
</el-form-item>
用mouseTool添加区域后开启编辑区域
methods:{
addArea(){
if (this.noClick == false) {
this.noClick = true
var self = this;
var mouseTool = null;
mouseTool = new AMap.MouseTool(this.map);
//同Polygon的Option设置
mouseTool.polygon({
fillColor:'#00b0ff',
strokeColor:'#80d8ff'
});
var edit;//开启区域编辑
var arr = [];//区域经纬度数据
//添加MouseTool事件监听
AMap.event.addListener( mouseTool,'draw',function(e){
//mouseTool每次只能画一个区域
if(self.map.getAllOverlays('polygon').length>0){
mouseTool.close(false);//true为清除覆盖物
}
//获取mouseTool画区域的经纬度
var mouseToolPoint = e.obj.getPath();
for (let i = 0; i < mouseToolPoint.length; i++) {
arr.push([mouseToolPoint[i].lng,mouseToolPoint[i].lat]);
}
//获取需要编辑的覆盖物
edit = new AMap.PolyEditor(self.map, self.map.getAllOverlays('polygon')[0]);
//编辑多边形
edit.open();
edit.on('adjust', function(event) {
arr = [];
for(var i=0;i<mouseToolPoint.length;i++){
arr.push([mouseToolPoint[i].lng,mouseToolPoint[i].lat]);
}
//判断点是否在当前区域内
self.isNei(arr);
})
})
}
}
}
编辑获取后端返回的区域数据画区域及编辑区域
var polygonList = [[lon,lat],[lon,lat],[lon,lat]];
this.creatPolygon(polygonList);
creatPolygon(path){
var path_ = path;
var polygon = new AMap.Polygon({
map: this.map,
path: path_,//设置多边形边界路径
strokeColor: "#FF33FF", //线颜色
strokeOpacity: 0.2, //线透明度
strokeWeight: 3, //线宽
fillColor: "#1791fc", //填充色
fillOpacity: 0.35//填充透明度
});
polygon['layerType'] = '区域矩形';
//也可以经度纬度单独相加除经纬度的个数求平均值就是中心点
this.map.setZoomAndCenter(11, [path[0].lng, path[0].lat]);
var arr = [];
var self = this;
setTimeout(()=>{
this.isNei(this.initpath);//编辑初始化
var allOverlay = this.map.getAllOverlays();
for (let i = 0; i < allOverlay.length; i++) {
if(allOverlay[i].layerType == '区域矩形'){
var cur = allOverlay[i];
var edit = new AMap.PolyEditor(this.map, cur);
edit.open();
let polygonPath = polygon.getPath();
edit.on('adjust', function(event) {
arr = [];
for(var i=0;i<polygonPath.length;i++){
arr.push([polygonPath[i].lng,polygonPath[i].lat]);
}
self.isNei(arr);
})
}
}
},1000);
}
isNei 判断marker覆盖物是否处于多边形区域内,在区域内的点更换样式
isNei(){
for (let i = 0; i < this.lucePoint.length; i++) {
var selectPoint = [];//当前区域内的点
//高德后端数据需要转换一次02坐标
let gcj02 = wgs84togcj02(Number(this.lucePoint[i].longitude), Number(this.lucePoint[i].latitude));
var point = [gcj02[0],gcj02[1]];
//isPointInRing为true 当前坐标则在当前多边形区域内
var isPointInRing = AMap.GeometryUtil.isPointInRing(point,polygonArr);
if(isPointInRing){
selectPoint.push(this.lucePoint[i]);
}
}
//划区域结束后对框选的点进行样式更改 区域内的点更换样式图片
for (let i = 0; i < this.markerList.length; i++) {
var curId = this.markerList[i].getExtData().id;
//所有坐标先初始化最初样式
this.markerList[i].setContent('<div style="position: relative;width: 20px;height: 36px;">' +
'<div style="width: 20px;">' +
'<img style="width: 25px;height: 38px;" src="'+iDefault+'">' +
'</div>' +
'</div>');
for (let k = 0; k < selectPoint.length; k++) {
//循环都是当前区域内的点
if(curId == selectPoint[k].rspId){
this.markerList[i].setContent('<div>' +
'<div style="width: 20px;">' +
'<img style="width: 25px;height: 38px;" src="'+select+'">' +
'</div>' +
'</div>')
}
}
}
}
高德2.0版本变化
多边形编辑工具名称的变化PolyEditor - PolygonEditor
注意:重置清空区域 的话 编辑状态开启是没法清除掉的 需要edit.close()关闭编辑状态在清除 1.4.x没这个问题,直接就能全清除掉; 2.0 编辑状态 白色点是坐标点 蓝色是未生成的点 需要拖拽一次变成自己的白色点后在拖动才会生效
<script src="https://webapi.amap.com/maps?
v=2.0&key=key&plugin=AMap.MouseTool,AMap.PolygonEditor"></script>
添加区域
addArea() {
if (this.lucePoint.length > 0) {
if (this.noClick == false) {
this.noClick = true
var self = this
var mouseTool = null
mouseTool = new AMap.MouseTool(this.map)
mouseTool.polygon({
fillColor: '#00b0ff',
strokeColor: '#80d8ff'
// 同Polygon的Option设置
})
var edit
var arr = []
/*
2.0变化
*/
mouseTool.on('draw', function(e) {
if (self.map.getAllOverlays('polygon').length > 0) {
mouseTool.close(false)// true为清除覆盖物
}
/*
2.0变化
*/
edit = new AMap.PolygonEditor(self.map, self.map.getAllOverlays('polygon')[0])
// 编辑多边形
edit.open();
var mouseToolPoint = e.obj.getPath();
for (let i = 0; i < mouseToolPoint.length; i++) {
arr.push([mouseToolPoint[i].lng, mouseToolPoint[i].lat])
}
self.isNei(arr)
edit.on('adjust', function(e) {
var temp = [];
/*
2.0变化 需要拿到编辑后区域最新的坐标组
*/
let newPolygon = self.map.getAllOverlays('polygon')[0].getPath();
for (let i = 0; i < newPolygon.length; i++) {
temp.push([newPolygon[i].lng,newPolygon[i].lat])
};
self.isNei(temp)
})
})
}
} else {
this.noClick = false
this.map.clearMap()
this.$message({
message: '当前区域没有路侧点',
type: 'warning'
})
}
},
编辑获取后端返回的区域数据画区域及编辑区域
var polygonList = eval(this.row.sceneRange);
this.creatPolygon(polygonList);
creatPolygon(path) {
var path_ = path;
console.log('path_',path_);
var polygon = new AMap.Polygon({
map: this.map,
path: path_, // 设置多边形边界路径
strokeColor: '#FF33FF', // 线颜色
strokeOpacity: 0.2, // 线透明度
strokeWeight: 3, // 线宽
fillColor: '#1791fc', // 填充色
fillOpacity: 0.35// 填充透明度
})
polygon['layerType'] = '区域矩形'
this.map.setZoomAndCenter(11, [path[0].lng, path[0].lat])
var arr = []
var self = this
setTimeout(() => {
this.isNei(this.initpath)// 编辑初始化
var allOverlay = this.map.getAllOverlays()
for (let i = 0; i < allOverlay.length; i++) {
if (allOverlay[i].layerType == '区域矩形') {
var cur = allOverlay[i]
var edit = new AMap.PolygonEditor(this.map, cur)
edit.open()
edit.on('adjust', function(event) {
/*
2.0变化 需要拿到编辑后区域最新的坐标组
*/
var temp = [];
let newPolygon = self.map.getAllOverlays('polygon')[0].getPath();
for (let i = 0; i < newPolygon.length; i++) {
temp.push([newPolygon[i].lng,newPolygon[i].lat])
};
self.isNei(temp)
})
}
}
}, 1000)
},
区域查找中心点
经度纬度单独相加除经纬度的个数求平均值就是中心点
//经纬度平均数找中心点
sumLngLat(arr) {
var s = 0;
var d = 0;
for (var i = arr.length - 1; i >= 0; i--) {
s += arr[i].lng;
d += arr[i].lat
}
var lnglat = [s / arr.length, d / arr.length];
return lnglat;
},