在百度地图上绘制多边形并保存绘制的点的经纬度

昨天波波记录了在高德地图上绘制多边形区域,今天我们继续记录在百度地图上绘制多边形并保存绘制的点的经纬度,本篇内容来自于网络,仅供学习参考。

源码参考:

  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5.         <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
  6.         <style type="text/css">
  7.             body, html{width: 100%;height: 100%;margin:0;font-family:"微软雅黑";}
  8.             #allmap {width: 100%; height:500px; overflow: hidden;}
  9.             #result {width:100%;font-size:12px;}
  10.             dl,dt,dd,ul,li{
  11.                 margin:0;
  12.                 padding:0;
  13.                 list-style:none;
  14.             }
  15.             p{font-size:12px;}
  16.             dt{
  17.                 font-size:14px;
  18.                 font-family:"微软雅黑";
  19.                 font-weight:bold;
  20.                 border-bottom:1px dotted #000;
  21.                 padding:5px 0 5px 5px;
  22.                 margin:5px 0;
  23.             }
  24.             dd{
  25.                 padding:5px 0 0 5px;
  26.             }
  27.             li{
  28.                 line-height:28px;
  29.             }
  30.         </style>
  31.         <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=25eb303c9c5df0ec2424fa86816437da"></script>
  32.         <!--加载鼠标绘制工具-->
  33.         <script type="text/javascript" src="http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.js"></script>
  34.         <link rel="stylesheet" href="http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.css" />
  35.         <!--加载检索信息窗口-->
  36.         <script type="text/javascript" src="http://api.map.baidu.com/library/SearchInfoWindow/1.4/src/SearchInfoWindow_min.js"></script>
  37.         <link rel="stylesheet" href="http://api.map.baidu.com/library/SearchInfoWindow/1.4/src/SearchInfoWindow_min.css" />
  38.         <script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script>
  39.         <title>鼠标绘制工具</title>
  40.     </head>
  41.     <body>
  42.         <div id="allmap" style="overflow:hidden;zoom:1;position:relative;">
  43.             <div id="map" style="height:100%;-webkit-transition: all 0.5s ease-in-out;transition: all 0.5s ease-in-out;"></div>
  44.         </div>
  45.         <div>
  46.             <input type="button" class="btn_gray"  value="添加多边形" onclick="addPolyline()">
  47.             <input type="button" class="btn_gray" value="保存数据" onclick="saveData()">
  48.         </div>
  49.         <script type="text/javascript">
  50.             //记录marker、label、polyline的个数
  51.             var NUM_MARKER = 0,
  52.                     NUM_LABEL = 0,
  53.                     NUM_POLYLINE = 0;
  54.             var polyDefaultStyle = {
  55.                 strokeColor: "#f00",
  56.                 strokeOpacity: 0.6,
  57.                 strokeWeight: 4
  58.             }
  59.             /*
  60.              *用于存储地图各个配置项的数据结构
  61.              *包括:地图中心点、地图的监听事件、地图的控件、地图上的覆盖物等信息
  62.              *用于获取代码的时候绘制地图
  63.              */
  64.             var config = {
  65.                 city: "北京",
  66.                 center_point: new BMap.Point(116.403874, 39.914889),
  67.                 zoom: 12,
  68.                 container_width: 700,
  69.                 container_height: 550,
  70.                 enableScrollWheelZoom: true,
  71.                 enableKeyboard: true,
  72.                 enableDragging: true,
  73.                 enableDoubleClickZoom: true,
  74.                 scale_control: {
  75.                     added: true,
  76.                     anchor: "BMAP_ANCHOR_BOTTOM_LEFT",
  77.                     type: "BMAP_UNIT_IMPERIAL"
  78.                 },
  79.                 nav_control: {
  80.                     added: true,
  81.                     anchor: "BMAP_ANCHOR_TOP_LEFT",
  82.                     type: "BMAP_NAVIGATION_CONTROL_LARGE"
  83.                 },
  84.                 overview_control: {
  85.                     added: true,
  86.                     anchor: "BMAP_ANCHOR_BOTTOM_RIGHT",
  87.                     isopen: true
  88.                 },
  89.                 label_array: [],
  90.                 label_config: [],
  91.                 marker_array: [],
  92.                 marker_config: [],
  93.                 polyline_config: [],
  94.                 polyline_array: [],
  95.                 polyline_name_array: []
  96.             }
  97.             // 百度地图API功能
  98.             var map = new BMap.Map('map');
  99.             var poi = new BMap.Point(116.307852, 40.057031);
  100.             map.centerAndZoom(poi, 16);
  101.             map.enableScrollWheelZoom();
  102.             function drawMap(areaStr) {
  103.                 var areaObj = JSON.parse(areaStr);
  104.                 if (areaObj) {
  105.                     //清空服务范围缓存
  106.                     config.polyline_array = [];
  107.                     //对服务范围数据进行遍历
  108.                     for (var len = 0; len < areaObj.service_area.length; len++) {
  109.                         var area_item = areaObj.service_area[len];
  110.                         if (area_item) {
  111.                             var polyPoint = [];
  112.                             var name = area_item.name;
  113.                             var points = area_item.points;
  114.                             for (var p_num = 0; p_num < points.length; p_num++) {
  115.                                 var point = new BMap.Point(points[p_num].lng, points[p_num].lat);
  116.                                 polyPoint.push(point);
  117.                                 if (p_num == 0) {
  118.                                     map.centerAndZoom(point, 12);
  119.                                 }
  120.                             }
  121.                             //在地图上绘制服务范围区域
  122.                             var polygon = new BMap.Polygon(polyPoint, {
  123.                                 strokeColor: polyDefaultStyle.strokeColor,
  124.                                 strokeWeight: polyDefaultStyle.strokeWeight,
  125.                                 strokeOpacity: polyDefaultStyle.strokeOpacity});
  126.                             map.addOverlay(polygon);
  127.                             //将服务范围加入到缓存中
  128.                             config.polyline_array.push(polygon);
  129.                             config.polyline_name_array.push(name);
  130.                         }
  131.                     }
  132.                 }
  133.             }
  134.            var ranges = '{"service_area":[{"name":"多边形1","points":[{"lng":121.23036,"lat":31.218609},{"lng":121.233666,"lat":31.210579},{"lng":121.247177,"lat":31.206749},{"lng":121.276353,"lat":31.190811},{"lng":121.267442,"lat":31.237383}]}]}';
  135.             setTimeout(function () {
  136.                 drawMap(ranges);
  137.             }, 1000);
  138.         </script>
  139.         <script>
  140.             // 此变量在添加标注功能时,用于记录当前的click事件的处理函数
  141.             var clickHandler;
  142.             //鼠标样式
  143.             var cursorStyle = {
  144.                 "ol_marker": "hand",
  145.                 "ol_polygen": "crosshair",
  146.                 "ol_label": "text",
  147.                 "default": "auto"
  148.             };
  149.             //添加标注时,鼠标的label信息
  150.             var labelInfo = {
  151.                 "ol_marker": "左键标记,右键退出",
  152.                 "ol_polygen": "左键单击开始画线,双击结束画线,右键退出",
  153.                 "ol_label": "左键标记,右键退出",
  154.                 "drawing_line": "双击结束画线"
  155.             };
  156.             //显示鼠标提示信息
  157.             function cursorLableShow() {
  158.                 cursorLabel.show();
  159.             }
  160.             //隐藏鼠标提示信息
  161.             function cursorLabelHide() {
  162.                 cursorLabel.hide();
  163.             }
  164.             //声明和初始化跟随鼠标移动的label
  165.             var cursorLabel = new BMap.Label();
  166.             cursorLabel.setOffset(new BMap.Size(10, 10));
  167.             cursorLabel.hide();
  168.             //退出标注的绘制。需要清除掉map上click的处理函数。并把鼠标设置为默认的样式
  169.             function exitDrawing(handlerToRemove) {
  170.                 cursorLabel.hide();
  171.                 map.setDefaultCursor(cursorStyle["default"]);
  172.                 map.removeEventListener("click", handlerToRemove);
  173.                 map.removeEventListener("mouseout", cursorLabelHide);
  174.                 map.removeEventListener("mouseover", cursorLableShow);
  175.             }
  176.             //添加polyline的具体操作
  177.             function addPolyline() {
  178. //                initPanel();
  179.                 //如果click已经有事件处理函数,先清除掉
  180.                 if (clickHandler)
  181.                     exitDrawing(clickHandler);
  182.                 map.setDefaultCursor(cursorStyle["ol_polygen"]);
  183.                 var polyPoint = [];
  184.                 var polyline = null;
  185.                 /*
  186.                  * click事件的监听事件
  187.                  * 如果没有初始化polyline变量,则初始化polyline并添加到地图
  188.                  * 如果已经初始化了polyline变量,则将click的经纬度加到polyline 的路径中,并重新设定设定polyline的path
  189.                  */
  190.                 function addPolyClickHandler(e) {
  191.                     cursorLabel.setContent(labelInfo["drawing_line"]);
  192.                     var point = e.point;
  193.                     polyPoint.push(point);
  194.                     //画多边形
  195.                     if (!polyline) {
  196.                         polyline = new BMap.Polygon(polyPoint, {
  197.                             strokeColor: polyDefaultStyle.strokeColor,
  198.                             strokeWeight: polyDefaultStyle.strokeWeight,
  199.                             strokeOpacity: polyDefaultStyle.strokeOpacity});
  200.                         polyline.setPath(polyPoint);
  201.                         map.addOverlay(polyline);
  202.                         polyPoint.length++;
  203.                     } else {
  204.                         polyline.setPath(polyPoint);
  205.                     }
  206.                 }
  207.                 /*
  208.                  * 双击(dbclick)事件的监听事件
  209.                  * 清除为绘制polyline添加的几个监听函数
  210.                  * 将polyline变量保存到config变量中
  211.                  */
  212.                 function addPolyDdclickHandler(e) {
  213.                     exitDrawing(addPolyClickHandler);
  214.                     if (polyline) {
  215.                         polyline.addEventListener("mouseover", function (e) {
  216.                             polyline.enableEditing();
  217.                         });
  218.                         polyline.addEventListener("mouseout", function (e) {
  219.                             polyline.disableEditing();
  220.                         });
  221.                         config.polyline_array.push(polyline);
  222.                         config.polyline_config.push({path: polyline.getPath(),
  223.                             strokeColor: polyDefaultStyle.strokeColor,
  224.                             strokeWeight: polyDefaultStyle.strokeWeight,
  225.                             strokeOpacity: polyDefaultStyle.strokeOpacity});
  226.                         var name = "多边形" + config.polyline_array.length;
  227.                         config.polyline_name_array.push(name);
  228.                     }
  229.                     map.removeEventListener("click", addPolyClickHandler);
  230.                     map.removeEventListener("mousemove", polyMoveHandler);
  231.                     map.removeEventListener("dblclick", addPolyDdclickHandler);
  232.                     map.removeEventListener("rightclick", polyRemove);
  233.                     //如果有允许双击放大地图,则重新加上
  234.                     setTimeout(function () {
  235.                         if (config.enableDoubleClickZoom)
  236.                             map.enableDoubleClickZoom();
  237.                     }, 1000);
  238.                 }
  239.                 //鼠标移动事件的监听函数
  240.                 function polyMoveHandler(e) {
  241.                     if (!polyline)
  242.                         return;
  243.                     if (polyPoint.length > 0) {
  244.                         polyPoint[polyPoint.length - 1] = e.point;
  245.                         cursorLabel.show();
  246.                         cursorLabel.setPosition(e.point);
  247.                         polyline.setPath(polyPoint);
  248.                     }
  249.                 }
  250.                 //取消绘制折线
  251.                 function polyRemove(e) {
  252.                     map.removeOverlay(polyline);
  253.                     exitDrawing(addPolyClickHandler);
  254.                     //如果有允许双击放大地图,则重新加上
  255.                     setTimeout(function () {
  256.                         if (config.enableDoubleClickZoom)
  257.                             map.enableDoubleClickZoom();
  258.                     }, 1000);
  259.                 }
  260.                 clickHandler = addPolyClickHandler;
  261.                 map.disableDoubleClickZoom();
  262.                 map.addEventListener("click", addPolyClickHandler);
  263.                 map.addEventListener("mousemove", polyMoveHandler);
  264.                 map.addEventListener("dblclick", addPolyDdclickHandler);
  265.                 map.addEventListener("rightclick", polyRemove);
  266.             }
  267.             /*
  268.              * 保存服务范围数据
  269.              */
  270.             function saveData() {
  271.                 if (config.polyline_array) {
  272.                     var area = {
  273.                         "service_area": []
  274.                     };
  275.                     for (var len = 0; len < config.polyline_array.length; len++) {
  276.                         var pl = config.polyline_array[len];
  277.                         if (pl != null) {
  278.                             var points_json = [];
  279.                             var points = pl.getPath();
  280.                             for (var p_num = 0; p_num < points.length; p_num++) {
  281.                                 var point = {
  282.                                     "lng": points[p_num].lng,
  283.                                     "lat": points[p_num].lat
  284.                                 }
  285.                                 points_json[p_num] = point;
  286.                             }
  287.                             var item_json = {
  288.                                 "name": config.polyline_name_array[len],
  289.                                 "points": points_json
  290.                             }
  291.                             area.service_area[len] = item_json;
  292.                         }
  293.                     }
  294.                     localStorage.setItem("area", JSON.stringify(area));
  295.                     var gid = "2318";  //getQueryString("gid")
  296.                     var city = "上海";  // getQueryString("city");
  297. //                    var ranges = JSON.stringify(area);
  298.                      var ranges = [{"city":city,"ranges":JSON.stringify(area)}];
  299.                     var url = "/?ranges=" + JSON.stringify(ranges);
  300.                     $.ajax({
  301.                         type: 'get',
  302.                         url: url,
  303.                         data: {},
  304.                         dataType: 'json',
  305.                         async: false, //同步
  306.                         crossDomain: true,
  307.                         withCredentials: true,
  308.                         success: function (data) {
  309.                             if (data.code == 0) {
  310.                                 alert("保存成功");
  311.                             }
  312.                         },
  313.                         error: function (error, type) {
  314.                             alert("type:" + type + "error:" + error);
  315.                             if (type == "abort") {
  316.                                 showAbortToast();
  317.                             } else if (type == "timeout") {
  318.                                 showTimeoutToast();
  319.                             }
  320.                         }
  321.                     });
  322.                 }
  323.             }
  324.         </script>
  325.     </body>
  326. </html>

演示效果:

在百度地图上绘制多边形并保存绘制的点的经纬度

还有一个编辑和保存多边形文件是复制百度地图生成器样式修改的。百度地图生成器:http://api.map.baidu.com/lbsapi/createmap/

演示效果:

百度地图上绘制多边形并保存绘制的点的经纬度

源码下载地址:https://www.zkii.net/download/code/baiduPolyLine.7z

2017-03-2更新:升级版百度多边形编辑,效果不再放了。源码下载地址:https://www.zkii.net/download/code/baiduPolyLine2.7z

 

以上内容转自:https://www.cnblogs.com/dcb3688/p/4608026.html

你想把广告放到这里吗?

发表评论

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