百度地图-获取地区轮廓线-画出城市或市区的范围

 

输入省、直辖市或县名称:

<script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=E38e696a2f91f7f3cd2eb14a77618305"></script>
<style type="text/css">body{font-size:13px;margin:10px}#container{width:700px;height:600px;border:1px solid gray}</style>

&nbsp;
<div id="container"></div>
输入省、直辖市或县名称:<input id="districtName" style="width: 100px;" type="text" value="北京市" />
<input type="button" value="获取轮廓线" onclick="getBoundary()"/>
<script type="text/javascript">
var map = new BMap.Map("container");
map.centerAndZoom(new BMap.Point(116.331398, 39.897445), 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;
    bdary.get(name, function(rs){       //获取行政区域
        map.clearOverlays();        //清除地图覆盖物       
        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>