高德地图画多边形地理围栏并保存边界经纬度

在即时配送、共享单车等应用中,我们通常需要为目标划定地图的边界范围。在查阅了相关资料后发现高德地图早在多年前就已经有了相关的组件。这篇笔记在查阅了高德开放平台的相关API以及参阅了时间博客的一篇文章,链接地址:https://www.timeblog.cn/article/137.html

效果演示:

高德地图画多边形地理围栏并保存边界经纬度

这里主要用到的是高德的PolyEditor多边形编辑器,除此之外还可以画点、线、圆形、矩形。具体去高德开放平台查看即可。

参考源码:

  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="closeEditPolygon();"/>
  20. </div>
  21. <script>
  22.     var storage = window.localStorage;
  23.     var editorTool, map = new AMap.Map("container", {
  24.         resizeEnable: true,
  25.         //center: [116.403322, 39.900255],//地图中心点
  26.         zoom: 13 //地图显示的缩放级别
  27.     });
  28.     var beginNum = 0;
  29.     var clickListener ;
  30.     var beginPoints;//存坐标的数组
  31.     var beginMarks ;
  32.     var polygonEditor;
  33.     var resPolygon = [];
  34.     var resNum = 0;
  35.     var editRegion = storage.getItem("FenceRegion");
  36.     init();
  37.     function init(){
  38.         beginPoints = [];
  39.         beginMarks = [];
  40.         beginNum = 0;
  41.         polygonEditor = '';
  42.         if(editRegion){
  43.             beginPoints = JsonToArray(editRegion);
  44.             console.log(beginPoints);
  45.             var polygon = createPolygon(beginPoints);
  46.             polygonEditor = createEditor(polygon);//如果是要不可编辑状态去掉此行
  47.         }else{
  48.             clickListener = AMap.event.addListener(map, "click", mapOnClick);
  49.         }
  50.     }
  51.     function mapOnClick(e) {
  52.         beginMarks.push(addMarker(e.lnglat));
  53.         console.log(e.lnglat);
  54.         beginPoints.push(e.lnglat);
  55.         beginNum++;
  56.         if(beginNum == 3){
  57.             AMap.event.removeListener(clickListener);
  58.             var polygon = createPolygon(beginPoints);
  59.             polygonEditor = createEditor(polygon);
  60.             clearMarks();
  61.         }
  62.     };
  63.     /*创建多边形*/
  64.     function createPolygon(arr){
  65.         var polygon = new AMap.Polygon({
  66.             map: map,
  67.             path: arr,
  68.             strokeColor: "#0000ff",
  69.             strokeOpacity: 1,
  70.             strokeWeight: 3,
  71.             fillColor: "#f5deb3",
  72.             fillOpacity: 0.35
  73.         });
  74.         return polygon;
  75.     }
  76.     function createEditor(polygon){
  77.         var polygonEditor = new AMap.PolyEditor(map, polygon);
  78.         polygonEditor.open();
  79.         AMap.event.addListener(polygonEditor,'end',polygonEnd);
  80.         return polygonEditor;
  81.     }
  82.     function closeEditPolygon(){
  83.         if(!polygonEditor){
  84.             console.log('未获取到地图');
  85.             return false;
  86.         }
  87.         //polygonEditor.close();//关闭编辑状态
  88.         var FenceRegion = [];
  89.         if(!beginPoints){
  90.             console.log('未获取到坐标');
  91.             return false;
  92.         }
  93.         beginPoints.forEach(function(item,index){
  94.             var lng = parseFloat(item['lng']);//经
  95.             var lat = parseFloat(item['lat']);//纬
  96.             FenceRegion.push([lng,lat]);
  97.         });
  98.         var newRegion = ObjectToJson(FenceRegion);
  99.         console.log(newRegion);
  100.         // [
  101.         //     {"lng":106.468742,"lat":29.543236},
  102.         //     {"lng":106.470115,"lat":29.522475},
  103.         //     {"lng":106.499298,"lat":29.533827}
  104.         // ]
  105.         /*给父页设置值*/
  106.         parent.$('input[name=fence_region]').val(newRegion);
  107.         storage.setItem("FenceRegion",newRegion);
  108.     }
  109.     function polygonEnd(res){
  110.         resPolygon.push(res.target);
  111.         alert(resPolygon[resNum].contains([116.38632839.913818]));
  112.         appendHideHtml(resNum,res.target.getPath());
  113.         resNum++;
  114.         init();
  115.     }
  116.     function appendHideHtml(index,arr){
  117.         var strify = JSON.stringify(arr);
  118.         var html = '<input type="hidden" id="index'+index+'" name="paths[]" value="'+strify+'">';
  119.         $('body').append(html);
  120.         console.log(html);
  121.     }
  122.     function clearMarks(){
  123.         map.remove(beginMarks);
  124.     }
  125.     function JsonToArray(json){
  126.         var arr = JSON.parse(json);
  127.         var res = [];
  128.         for (var i = 0; i < arr.length; i++) {
  129.             var line = [];
  130.             line.push(arr[i].lng);
  131.             line.push(arr[i].lat);
  132.             res.push(line);
  133.         };
  134.         return res;
  135.     }
  136.     // 实例化点标记
  137.     function addMarker(lnglat) {
  138.         var marker = new AMap.Marker({
  139.             icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
  140.             position: lnglat
  141.         });
  142.         marker.setMap(map);
  143.         return marker;
  144.     }
  145.     function ObjectToJson(arr){
  146.         if(!arr.length) return null;
  147.         var i = 0;
  148.         len = arr.length,
  149.             array = [];
  150.         for(;i<len;i++){
  151.             array.push({"lng":arr[i][0],"lat":arr[i][1]});
  152.         }
  153.         return JSON.stringify(array);
  154.     }
  155. </script>
  156. </body>
  157. </html>

地图边界范围选定后,上传后台保存即可。

 

你想把广告放到这里吗?

发表评论

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