高德地图画多边形地理围栏(完美修改版)

今天上午的时候菠菜园记录了一份《高德地图画多边形地理围栏并保存边界经纬度》,尽管实现了在高德地图上绘制多边形区域,但是在实际使用中灵活性不是很高。因此在查阅了高德相关API后,对上一篇笔记中的代码进行了二次修改。增加了多边形的拖动,拖动后边界经纬度的获取,多边形区域的清除和重绘。以便用户在绘制错误后,可以不用通过刷新页面就能随时、任意地修改。

高德地图画多边形地理围栏

新增图标:https://www.zkii.net/wp-content/uploads/2023/03/map_marker.png

参考源码:

  1. <html>
  2. <head>
  3.     <meta charset="utf-8">
  4.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5.     <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
  6.     <title>编辑折线、多边形、圆</title>
  7.     <link rel="stylesheet" href="https://cache.amap.com/lbs/static/main1119.css"/>
  8.     <script src="https://webapi.amap.com/maps?v=1.4.4&key=你申请的Key&plugin=AMap.PolyEditor"></script>
  9.     <script type="text/javascript" src="https://cache.amap.com/lbs/static/addToolbar.js"></script>
  10.     <script src="https://lib.sinaapp.com/js/jquery/2.2.4/jquery-2.2.4.min.js" type="text/javascript" charset="utf-8"></script>
  11.     <script src="https://a.amap.com/jsapi_demos/static/demo-center/js/demoutils.js"></script>
  12.     <style type="text/css" media="screen">
  13.         .button-group {position: absolute;top: 0px;right: 20px;font-size: 12px;padding: 10px;}
  14.     </style>
  15. </head>
  16. <body>
  17. <div id="container"></div>
  18. <div class="button-group">
  19.     <input type="button" class="button" value="清除当前区域" onClick="clearCurrentArea();"/>
  20.     <input type="button" class="button" value="区域编辑完成" onClick="closeEditPolygon();"/>
  21. </div>
  22. <script>
  23.     var storage = window.localStorage;
  24.     storage.removeItem('FenceRegion');
  25.     var editorTool, map = new AMap.Map("container", {
  26.         resizeEnable: true,
  27.         //center: [116.403322, 39.900255],//地图中心点
  28.         zoom: 13 //地图显示的缩放级别
  29.     });
  30.     var beginNum = 0;
  31.     var clickListener ;
  32.     var beginPoints;//存坐标的数组
  33.     var beginMarks ;
  34.     var polygonEditor;
  35.     var resPolygon = [];
  36.     var resNum = 0;
  37.     var editRegion = storage.getItem("FenceRegion");
  38.     var polygonObj;
  39.     init();
  40.     function init(){
  41.         beginPoints = [];
  42.         beginMarks = [];
  43.         beginNum = 0;
  44.         polygonEditor = '';
  45.         if(editRegion){
  46.             beginPoints = JsonToArray(editRegion);
  47.             console.log(beginPoints);
  48.             var polygon = createPolygon(beginPoints);
  49.             polygonEditor = createEditor(polygon);//如果是要不可编辑状态去掉此行
  50.         }else{
  51.             clickListener = AMap.event.addListener(map, "click", mapOnClick);
  52.         }
  53.     }
  54.     //清除当前区域选择,并支持重新划定区域
  55.     function clearCurrentArea() {
  56.         map.remove(polygonObj)
  57.         storage.removeItem('FenceRegion');
  58.         editRegion = storage.getItem("FenceRegion");
  59.         polygonEditor.close();
  60.         init()
  61.     }
  62.     function mapOnClick(e) {
  63.         beginMarks.push(addMarker(e.lnglat));
  64.         console.log(e.lnglat);
  65.         beginPoints.push(e.lnglat);
  66.         beginNum++;
  67.         if(beginNum == 3){
  68.             AMap.event.removeListener(clickListener);
  69.             var polygon = createPolygon(beginPoints);
  70.             polygonEditor = createEditor(polygon);
  71.             clearMarks();
  72.         }
  73.     };
  74.     /*创建多边形*/
  75.     function createPolygon(arr){
  76.         var polygon = this.polygon = new AMap.Polygon({
  77.             map: map,
  78.             path: arr,
  79.             strokeColor: "#f8484c",
  80.             strokeOpacity: 1,
  81.             strokeWeight: 3,
  82.             fillColor: "#f8484c",
  83.             fillOpacity: 0.35,
  84.             draggable:true
  85.         });
  86.         polygonObj = polygon;
  87.         return polygon;
  88.     }
  89.     function createEditor(polygon){
  90.         var polygonEditor = new AMap.PolyEditor(map, polygon);
  91.         polygonEditor.open();
  92.         AMap.event.addListener(polygonEditor,'end',polygonEnd);
  93.         AMap.event.addListener(polygon,'dragend',DragEnd);
  94.         return polygonEditor;
  95.     }
  96.     //多边形拖拽后,获取新的轮廓坐标
  97.     function DragEnd(r){
  98.         var pathArr = r.target.getPath();
  99.         var FenceRegion = [];
  100.         beginPoints = [];
  101.         pathArr.forEach(function (item) {
  102.             var lng = parseFloat(item['lng']);
  103.             var lat = parseFloat(item['lat']);
  104.             FenceRegion.push([lng,lat]);
  105.             beginPoints.push(item);
  106.         })
  107.         var newRegion = ObjectToJson(FenceRegion);
  108.         storage.setItem("FenceRegion",newRegion);
  109.     }
  110.     function closeEditPolygon(){
  111.         if(!polygonEditor){
  112.             console.log('未获取到地图');
  113.             return false;
  114.         }
  115.         //polygonEditor.close();//关闭编辑状态
  116.         var FenceRegion = [];
  117.         if(!beginPoints){
  118.             console.log('未获取到坐标');
  119.             return false;
  120.         }
  121.         beginPoints.forEach(function(item,index){
  122.             var lng = parseFloat(item['lng']);//经
  123.             var lat = parseFloat(item['lat']);//纬
  124.             FenceRegion.push([lng,lat]);
  125.         });
  126.         var newRegion = ObjectToJson(FenceRegion);
  127.         console.log(newRegion);
  128.         storage.setItem("FenceRegion",newRegion);
  129.     }
  130.     function polygonEnd(res){
  131.         resPolygon.push(res.target);
  132.         alert(resPolygon[resNum].contains([longitude, latitude]));
  133.         appendHideHtml(resNum,res.target.getPath());
  134.         resNum++;
  135.         init();
  136.     }
  137.     function clearMarks(){
  138.         map.remove(beginMarks);
  139.     }
  140.     function JsonToArray(json){
  141.         var arr = JSON.parse(json);
  142.         var res = [];
  143.         for (var i = 0; i < arr.length; i++) {
  144.             var line = [];
  145.             line.push(arr[i].lng);
  146.             line.push(arr[i].lat);
  147.             res.push(line);
  148.         };
  149.         return res;
  150.     }
  151.     // 实例化点标记
  152.     function addMarker(lnglat) {
  153.         var marker = new AMap.Marker({
  154.             icon: "https://www.zkii.net/wp-content/uploads/2023/03/map_marker.png",
  155.             position: lnglat
  156.         });
  157.         marker.setMap(map);
  158.         return marker;
  159.     }
  160.     function ObjectToJson(arr){
  161.         if(!arr.length) return null;
  162.         var i = 0;
  163.         len = arr.length,
  164.             array = [];
  165.         for(;i<len;i++){
  166.             array.push({"lng":arr[i][0],"lat":arr[i][1]});
  167.         }
  168.         return JSON.stringify(array);
  169.     }
  170. </script>
  171. </body>
  172. </html>

 

你想把广告放到这里吗?

发表评论

您必须 登录 才能发表留言!