百度地圖獲取多行政區域圍欄

胡辣湯多放醋發表於2020-12-25
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>獲取地區輪廓線</title>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.3">
    </script>
    <style type="text/css">
        body {
            font-size: 13px;
            margin: 10px
        }

        #container {
            width: 1500px;
            height: 500px;
            border: 1px solid gray
        }
    </style>
</head>
<body>
<div id="container"></div>
<div id="controler">
    輸入省、直轄市或縣名稱:<input type="text" id="districtName" style="width:180px" value="重慶市">
    <input type="button" onclick="getBoundary()" value="獲取輪廓線"></div>

<script type="text/javascript">
    var map = new BMap.Map("container");
    map.centerAndZoom(new BMap.Point(116.403765, 39.914850), 5);
    map.addControl(new BMap.NavigationControl({type: BMAP_NAVIGATION_CONTROL_SMALL}));
    map.enableScrollWheelZoom();

    function getBoundary() {
        var bdary = new BMap.Boundary();
        var name = document.getElementById("districtName").value;
        var split = name.split("、");
        map.clearOverlays();        //清除地圖覆蓋物
        for (var j = 0; j < split.length ; j++) {
            bdary.get(split[j], function (rs) {       //獲取行政區域
                var count = rs.boundaries.length; //行政區域的點有多少個
                for (var i = 0; i < count; i++) {
                    var ply = new BMap.Polygon(rs.boundaries[i], {strokeWeight: 2, strokeColor: "#ff0000"}); //建立多邊形覆蓋物
                    map.addOverlay(ply);  //新增覆蓋物
                    // map.setViewport(ply.getPath(),);    //調整視野
                }
            });
        }

    }
</script>
</body>
</html>

相關文章