百度地圖 多點規划路徑(多個坐標點連接)


這一版比較簡陋,並且沒有做數字限制,也只能傳偶數個坐標點,后期繼續更新

百度地圖坐標點拾取地址

http://api.map.baidu.com/lbsapi/getpoint/index.html

<!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=utf8" />
	<link rel="stylesheet" type="text/css" href="css/index.css"/>
</head>
<body>
  <!--百度地圖容器-->
  <div style="width:99.9%;height:600px;border:#ccc solid 1px;" id="dituContent"></div>
  <div id="location">
  	坐標點
  	<div id="location-list">
  		<dl class="point1">
  			<dt>縱坐標:<span class="xp">116.301934</span></dt>
  			<dd>橫坐標:<span class="yp">39.977552</span></dd>
  		</dl>
  	</div>
  	<div id="ipt-location">
  		<dl>
  			<dt>輸入縱坐標:<input type="text" name="" id="xPoint" value="" /></dt>
  			<dd>輸入橫坐標:<input type="text" name="" id="yPoint" value="" /></dd>
  		</dl>
  		<div id="addPoint">添加線路點</div>
  	</div>
  </div>
  <div id="btn">
  	點擊我生成路線
  </div>
</body>
<script src="js/jquery-3.3.1.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" src="http://api.map.baidu.com/api?ak=UAumsTfvpKBlNPUd9e5PvAEnR0YGNllO&v=2.0&services=true"></script>
<script type="text/javascript">
    //創建和初始化地圖函數:
    function initMap(){
        createMap();//創建地圖
        setMapEvent();//設置地圖事件
        addMapControl();//向地圖添加控件
    }
    
    //創建地圖函數:
    function createMap(){
        var map = new BMap.Map("dituContent");//在百度地圖容器中創建一個地圖
        var point = new BMap.Point(116.301934,39.977552);//定義一個中心點坐標
        map.centerAndZoom(point,12);//設定地圖的中心點和坐標並將地圖顯示在地圖容器中
        window.map = map;//將map變量存儲在全局
    }
    
    //地圖事件設置函數:
    function setMapEvent(){
        map.enableDragging();//啟用地圖拖拽事件,默認啟用(可不寫)
        map.enableScrollWheelZoom();//啟用地圖滾輪放大縮小
        map.enableDoubleClickZoom();//啟用鼠標雙擊放大,默認啟用(可不寫)
        map.enableKeyboard();//啟用鍵盤上下左右鍵移動地圖
    }
    
    //地圖控件添加函數:
    function addMapControl(){
        //向地圖中添加縮放控件
	var ctrl_nav = new BMap.NavigationControl({anchor:BMAP_ANCHOR_TOP_LEFT,type:BMAP_NAVIGATION_CONTROL_LARGE});
	map.addControl(ctrl_nav);
        //向地圖中添加縮略圖控件
	var ctrl_ove = new BMap.OverviewMapControl({anchor:BMAP_ANCHOR_BOTTOM_RIGHT,isOpen:1});
	map.addControl(ctrl_ove);
        //向地圖中添加比例尺控件
	var ctrl_sca = new BMap.ScaleControl({anchor:BMAP_ANCHOR_BOTTOM_LEFT});
	map.addControl(ctrl_sca);
    }
    
    
    initMap();//創建和初始化地圖
    
    var _index = 0;
    //相鄰坐標點 
    var pointArray = [];
    var allMapPointArray = [];
    $("#addPoint").click(function(){
    	var xpoint = $("#xPoint").val().trim();
    	var ypoint = $('#yPoint').val().trim();
    	pointArray.push(new BMap.Point(xpoint, ypoint));
    	
    	//dom添加
    	var temp =  $(".point1").eq(0).clone(true,true);
    	temp.find('.xp').html(xpoint);
    	temp.find('.yp').html(ypoint);
    	$("#location-list").append(temp)
    });
    
    $('#btn').on('click', function () {
    	//pointArray.push(new BMap.Point(122.361609,37.388692))
    	//pointArray.push(new BMap.Point(122.55612,37.373781))
      	//pointArray.push(new BMap.Point(122.573276,37.248397))
      	//pointArray.push(new BMap.Point(122.493441,37.170755))

        var driving = new BMap.DrivingRoute(map,{
                policy: BMAP_DRIVING_POLICY_LEAST_DISTANCE,
            }
        );


			
		var pointTwoArray = [];
		for (var i=0;i<pointArray.length;i++) {
			//是否是奇數點
			//是否數組越界
			if(i!=(pointArray.length-1)){
				pointTwoArray.push(pointArray.slice(i,i+2));
			}
		}
		
		pointTwoArray.forEach(function(value,index){
			_index++;
			driving.search(value[0],value[1]);
		});
		driving.setSearchCompleteCallback(function(){
			 var pts = driving.getResults().getPlan(0).getRoute(0).getPath();
				var polyline = new BMap.Polyline(pts, {strokeColor:"yellowgreen",//設置顏色 
			        strokeWeight:3, //寬度
			        strokeOpacity:1.0});//透明度
			 map.addOverlay(polyline);
			 
			 map.centerAndZoom(pts[0],12);
		});
    });
    

</script>
</html>

  

 

css文件

* {
  margin: 0;
  padding: 0;
  border: 0;
}
#btn {
  width: 300px;
  height: 60px;
  text-align: center;
  line-height: 60px;
  background: skyblue;
  border-radius: 15px;
  color: #fff;
  font-weight: bold;
  margin: 0 auto;
  margin-top: 60px;
}
#location {
  text-align: center;
  font-size: 30px;
}
#location > #location-list > dl {
  margin: 0 auto;
  width: 30%;
  text-align: left;
  border: 1px solid #ccc;
  height: 45px;
  line-height: 45px;
  font-size: 0px;
}
#location > #location-list > dl > dt {
  display: inline-block;
  font-size: 16px;
  vertical-align: top;
  width: 50%;
}
#location > #location-list > dl > dd {
  display: inline-block;
  font-size: 16px;
  vertical-align: top;
  width: 50%;
}
#location > #location-list > dl:first-child {
  display: none;
}
#location > #ipt-location {
  width: 30%;
  margin: 0 auto;
  line-height: 60px;
  border: 1px solid #ccc;
  margin-top: 30px;
}
#location > #ipt-location > dl {
  text-align: left;
  font-size: 0px;
}
#location > #ipt-location > dl > dt {
  display: inline-block;
  font-size: 16px;
  vertical-align: top;
  width: 50%;
}
#location > #ipt-location > dl > dt > input {
  height: 40px;
  border: 1px solid #ccc;
}
#location > #ipt-location > dl > dd {
  display: inline-block;
  font-size: 16px;
  vertical-align: top;
  width: 50%;
}
#location > #ipt-location > dl > dd > input {
  height: 40px;
  border: 1px solid #ccc;
}
#location > #ipt-location > #addPoint {
  width: 120px;
  height: 45px;
  font-size: 16px;
  border: 1px solid #87CEEB;
  vertical-align: top;
  line-height: 45px;
  margin: 0 auto;
  cursor: pointer;
}
/*# sourceMappingURL=index.css.map */

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM