How to obtain the geographical location of each province, city, and city in the country on Baidu Map (map-JSON-point-administrative area)

How to get the district and county points of Baidu map?

Open the Baidu map development platform, copy the code into the red box and click to run, pay attention to input the administrative area you need, and support provinces, cities and counties.

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
        body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
    </style>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
    <title>添加行政区划</title>
</head>
<body>
    <div id="allmap"></div>
  
</body>
</html>

<script type="text/javascript">
    // 百度地图API功能
    var map = new BMapGL.Map("allmap");
    map.centerAndZoom(new BMapGL.Point(116.403765, 39.914850), 5);
    map.enableScrollWheelZoom();

    function getBoundary(){       
        var bdary = new BMapGL.Boundary();
        bdary.get("天桥区", function(rs){       //获取行政区域
            map.clearOverlays();        //清除地图覆盖物       
            var count = rs.boundaries.length; //行政区域的点有多少个
          console.log(rs.boundaries);
         
          
          var content = rs.boundaries
          var fileName = 'test.txt'
          var aLink = document.createElement('a');
          var blob = new Blob([content]);
          var evt = document.createEvent("HTMLEvents");
          evt.initEvent("click", false, false);
          aLink.download = fileName;
          aLink.href = URL.createObjectURL(blob);
          aLink.dispatchEvent(evt);
          aLink.click();
          
          
            if (count === 0) {
                alert('未能获取当前输入行政区域');
                return ;
            }
              var pointArray = [];
            for (var i = 0; i < count; i++) {
                var ply = new BMapGL.Polygon(rs.boundaries[i], {strokeWeight: 2, strokeColor: "#ff0000"}); //建立多边形覆盖物
                map.addOverlay(ply);  //添加覆盖物
                pointArray = pointArray.concat(ply.getPath());
            }    
            map.setViewport(pointArray);    //调整视野  
            addlabel();               
        });   
    }

    setTimeout(function(){
        getBoundary();
    }, 2000);
</script>

Guess you like

Origin blog.csdn.net/z1093541823/article/details/124879621