百度地圖-多個坐標點顯示


做項目的時候需要用到的功能,一個頁面上,左邊是各個地點,右邊是地圖,一開始把所有的點全都標注在地圖上,點左邊的每個地點后地圖移動到以該點為中心,點擊地圖上的坐標點會顯示出相關的信息,

對着百度地圖的文檔一點一點做出來了,先記下來,以作備用!!!

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>百度地圖-多個坐標點一起顯示</title>
    <style type="text/css">
		 #allmap {width: 100%;height: 600px;overflow: hidden;margin:0;font-family:"微軟雅黑";}
	</style>
</head>
<body>
    <table style="width:100%">
        <tr>
        <td style="width:250px;"> 
            <ul>
                <li><a href="javascript:void(0)" onclick="to(116.404,39.915)">水廠1</a></li>
                <li><a href="javascript:void(0)" onclick="to(116.383752,39.91334)">水廠2</a></li>
                <li><a href="javascript:void(0)" onclick="to(116.384502,39.932241)">水廠3</a></li>
            </ul>
        </td>
        <td>
                <div id="allmap"></div>
        </td>
    </tr>
    </table>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=SyRux96r0i0Xue1Qqp0ZPb4uCc8BC6Aw"></script>
    <script type="text/javascript">
        // 百度地圖API功能
        var map = new BMap.Map("allmap");
        var point = new BMap.Point(116.404, 39.915);
        map.centerAndZoom(point, 15);
        
        var json_data = [[116.404,39.915,'水廠1<br />地址:中華路1號'],[116.383752,39.91334,'水廠2<br />地址:興寧路13號'],[116.384502,39.932241,'水廠3<br />地址:友愛路3-5號']];
        var pointArray = new Array();

	var opts = {
				width : 250,     // 信息窗口寬度
				height: 80,     // 信息窗口高度
				title : "" , // 信息窗口標題
				enableMessage:true//設置允許信息窗發送短息
			   };

        for(var i=0;i<json_data.length;i++){
            var marker = new BMap.Marker(new BMap.Point(json_data[i][0], json_data[i][1])); // 創建點
            map.addOverlay(marker);    //增加點
            pointArray[i] = new BMap.Point(json_data[i][0], json_data[i][1]);
            var content = json_data[i][2];
            addClickHandler(content,marker);
           
        }
        //讓所有點在視野范圍內
        map.setViewport(pointArray);

        	function addClickHandler(content,marker){
		marker.addEventListener("click",function(e){
			openInfo(content,e)}
		);
	}
	function openInfo(content,e){
		var p = e.target;
		var point = new BMap.Point(p.getPosition().lng, p.getPosition().lat);
		var infoWindow = new BMap.InfoWindow(content,opts);  // 創建信息窗口對象 
		map.openInfoWindow(infoWindow,point); //開啟信息窗口
	}
       	
//移動到某一坐標點
           function to(x,y){
            map.panTo(new BMap.Point(x,y)); 

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


免責聲明!

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



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