1. 程式人生 > >百度地圖選擇市區並去掉路線

百度地圖選擇市區並去掉路線

 <!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 BMap.Map("allmap");
map.centerAndZoom('貴陽', 12);
map.enableScrollWheelZoom();
  //去除路網
  map.setMapStyle({
        styleJson:[
          {
                    "featureType": "road",
                    "elementType": "all",
                    "stylers": {
                              //"color": "#ffffff",
                              "visibility": "off"
                    }
          }
]
});
  //單擊獲取點選的經緯度
map.addEventListener("click",function(e){
//alert(e.point.lng + "," + e.point.lat);
    var infoWindow = new BMap.InfoWindow(e.point.lng + ',' + e.point.lat); // 建立資訊視窗物件
    map.openInfoWindow(infoWindow, e.point); //開啟資訊視窗
});
  
function getBoundary(){       
var bdary = new BMap.Boundary();
bdary.get("雲巖區", function(rs){       //獲取行政區域
//map.clearOverlays();        //清除地圖覆蓋物       
var count = rs.boundaries.length; //行政區域的點有多少個
if (count === 0) {
alert('未能獲取當前輸入行政區域');
return ;
}
           var pointArray = [];
for (var i = 0; i < count; i++) {
var ply = new BMap.Polygon(rs.boundaries[i], {strokeWeight: 2, strokeColor: "#ff0000"}); //建立多邊形覆蓋物
map.addOverlay(ply);  //新增覆蓋物
pointArray = pointArray.concat(ply.getPath());
}    
map.setViewport(pointArray);    //調整視野                 
});
}

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