今天上午的时候菠菜园记录了一份《高德地图画多边形地理围栏并保存边界经纬度》,尽管实现了在高德地图上绘制多边形区域,但是在实际使用中灵活性不是很高。因此在查阅了高德相关API后,对上一篇笔记中的代码进行了二次修改。增加了多边形的拖动,拖动后边界经纬度的获取,多边形区域的清除和重绘。以便用户在绘制错误后,可以不用通过刷新页面就能随时、任意地修改。
新增图标:https://www.zkii.net/wp-content/uploads/2023/03/map_marker.png
参考源码:
- <html>
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
- <title>编辑折线、多边形、圆</title>
- <link rel="stylesheet" href="https://cache.amap.com/lbs/static/main1119.css"/>
- <script src="https://webapi.amap.com/maps?v=1.4.4&key=你申请的Key&plugin=AMap.PolyEditor"></script>
- <script type="text/javascript" src="https://cache.amap.com/lbs/static/addToolbar.js"></script>
- <script src="https://lib.sinaapp.com/js/jquery/2.2.4/jquery-2.2.4.min.js" type="text/javascript" charset="utf-8"></script>
- <script src="https://a.amap.com/jsapi_demos/static/demo-center/js/demoutils.js"></script>
- <style type="text/css" media="screen">
- .button-group {position: absolute;top: 0px;right: 20px;font-size: 12px;padding: 10px;}
- </style>
- </head>
- <body>
- <div id="container"></div>
- <div class="button-group">
- <input type="button" class="button" value="清除当前区域" onClick="clearCurrentArea();"/>
- <input type="button" class="button" value="区域编辑完成" onClick="closeEditPolygon();"/>
- </div>
- <script>
- var storage = window.localStorage;
- storage.removeItem('FenceRegion');
- var editorTool, map = new AMap.Map("container", {
- resizeEnable: true,
- //center: [116.403322, 39.900255],//地图中心点
- zoom: 13 //地图显示的缩放级别
- });
- var beginNum = 0;
- var clickListener ;
- var beginPoints;//存坐标的数组
- var beginMarks ;
- var polygonEditor;
- var resPolygon = [];
- var resNum = 0;
- var editRegion = storage.getItem("FenceRegion");
- var polygonObj;
- init();
- function init(){
- beginPoints = [];
- beginMarks = [];
- beginNum = 0;
- polygonEditor = '';
- if(editRegion){
- beginPoints = JsonToArray(editRegion);
- console.log(beginPoints);
- var polygon = createPolygon(beginPoints);
- polygonEditor = createEditor(polygon);//如果是要不可编辑状态去掉此行
- }else{
- clickListener = AMap.event.addListener(map, "click", mapOnClick);
- }
- }
- //清除当前区域选择,并支持重新划定区域
- function clearCurrentArea() {
- map.remove(polygonObj)
- storage.removeItem('FenceRegion');
- editRegion = storage.getItem("FenceRegion");
- polygonEditor.close();
- init()
- }
- function mapOnClick(e) {
- beginMarks.push(addMarker(e.lnglat));
- console.log(e.lnglat);
- beginPoints.push(e.lnglat);
- beginNum++;
- if(beginNum == 3){
- AMap.event.removeListener(clickListener);
- var polygon = createPolygon(beginPoints);
- polygonEditor = createEditor(polygon);
- clearMarks();
- }
- };
- /*创建多边形*/
- function createPolygon(arr){
- var polygon = this.polygon = new AMap.Polygon({
- map: map,
- path: arr,
- strokeColor: "#f8484c",
- strokeOpacity: 1,
- strokeWeight: 3,
- fillColor: "#f8484c",
- fillOpacity: 0.35,
- draggable:true
- });
- polygonObj = polygon;
- return polygon;
- }
- function createEditor(polygon){
- var polygonEditor = new AMap.PolyEditor(map, polygon);
- polygonEditor.open();
- AMap.event.addListener(polygonEditor,'end',polygonEnd);
- AMap.event.addListener(polygon,'dragend',DragEnd);
- return polygonEditor;
- }
- //多边形拖拽后,获取新的轮廓坐标
- function DragEnd(r){
- var pathArr = r.target.getPath();
- var FenceRegion = [];
- beginPoints = [];
- pathArr.forEach(function (item) {
- var lng = parseFloat(item['lng']);
- var lat = parseFloat(item['lat']);
- FenceRegion.push([lng,lat]);
- beginPoints.push(item);
- })
- var newRegion = ObjectToJson(FenceRegion);
- storage.setItem("FenceRegion",newRegion);
- }
- function closeEditPolygon(){
- if(!polygonEditor){
- console.log('未获取到地图');
- return false;
- }
- //polygonEditor.close();//关闭编辑状态
- var FenceRegion = [];
- if(!beginPoints){
- console.log('未获取到坐标');
- return false;
- }
- beginPoints.forEach(function(item,index){
- var lng = parseFloat(item['lng']);//经
- var lat = parseFloat(item['lat']);//纬
- FenceRegion.push([lng,lat]);
- });
- var newRegion = ObjectToJson(FenceRegion);
- console.log(newRegion);
- storage.setItem("FenceRegion",newRegion);
- }
- function polygonEnd(res){
- resPolygon.push(res.target);
- alert(resPolygon[resNum].contains([longitude, latitude]));
- appendHideHtml(resNum,res.target.getPath());
- resNum++;
- init();
- }
- function clearMarks(){
- map.remove(beginMarks);
- }
- function JsonToArray(json){
- var arr = JSON.parse(json);
- var res = [];
- for (var i = 0; i < arr.length; i++) {
- var line = [];
- line.push(arr[i].lng);
- line.push(arr[i].lat);
- res.push(line);
- };
- return res;
- }
- // 实例化点标记
- function addMarker(lnglat) {
- var marker = new AMap.Marker({
- icon: "https://www.zkii.net/wp-content/uploads/2023/03/map_marker.png",
- position: lnglat
- });
- marker.setMap(map);
- return marker;
- }
- function ObjectToJson(arr){
- if(!arr.length) return null;
- var i = 0;
- len = arr.length,
- array = [];
- for(;i<len;i++){
- array.push({"lng":arr[i][0],"lat":arr[i][1]});
- }
- return JSON.stringify(array);
- }
- </script>
- </body>
- </html>