<!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> <title>百度地圖API顯示多個標注點帶提示的代碼</title> <!--css--> <link href="style/demo.css" rel="stylesheet" type="text/css" /> <!--javascript--> <script src="scripts/jquery-1.9.1.js" type="text/javascript"></script> <script src="scripts/demo.js" type="text/javascript"></script> </head> <body> <div class="demo_main"> <fieldset class="demo_title"> 百度地圖API顯示多個標注點帶提示的代碼 </fieldset> <fieldset class="demo_content"> <div style="min-height: 300px; width: 100%;" id="map"> </div> <script type="text/javascript"> var markerArr = [ { title: "名稱:廣州火車站", point: "113.264531,23.157003", address: "廣東省廣州市廣州火車站", tel: "12306" }, { title: "名稱:廣州塔(赤崗塔)", point: "113.330934,23.113401", address: "廣東省廣州市廣州塔(赤崗塔) ", tel: "18500000000" }, { title: "名稱:廣州動物園", point: "113.312213,23.147267", address: "廣東省廣州市廣州動物園", tel: "18500000000" }, { title: "名稱:天河公園", point: "113.372867,23.134274", address: "廣東省廣州市天河公園", tel: "18500000000" } ]; function map_init() { var map = new BMap.Map("map"); // 創建Map實例 var point = new BMap.Point(113.312213, 23.147267); //地圖中心點,廣州市 map.centerAndZoom(point, 13); // 初始化地圖,設置中心點坐標和地圖級別。 map.enableScrollWheelZoom(true); //啟用滾輪放大縮小 //向地圖中添加縮放控件 var ctrlNav = new window.BMap.NavigationControl({ anchor: BMAP_ANCHOR_TOP_LEFT, type: BMAP_NAVIGATION_CONTROL_LARGE }); map.addControl(ctrlNav); //向地圖中添加縮略圖控件 var ctrlOve = new window.BMap.OverviewMapControl({ anchor: BMAP_ANCHOR_BOTTOM_RIGHT, isOpen: 1 }); map.addControl(ctrlOve); //向地圖中添加比例尺控件 var ctrlSca = new window.BMap.ScaleControl({ anchor: BMAP_ANCHOR_BOTTOM_LEFT }); map.addControl(ctrlSca); var point = new Array(); //存放標注點經緯信息的數組 var marker = new Array(); //存放標注點對象的數組 var info = new Array(); //存放提示信息窗口對象的數組 for (var i = 0; i < markerArr.length; i++) { var p0 = markerArr[i].point.split(",")[0]; // var p1 = markerArr[i].point.split(",")[1]; //按照原數組的point格式將地圖點坐標的經緯度分別提出來 point[i] = new window.BMap.Point(p0, p1); //循環生成新的地圖點 marker[i] = new window.BMap.Marker(point[i]); //按照地圖點坐標生成標記 map.addOverlay(marker[i]); marker[i].setAnimation(BMAP_ANIMATION_BOUNCE); //跳動的動畫 var label = new window.BMap.Label(markerArr[i].title, { offset: new window.BMap.Size(20, -10) }); marker[i].setLabel(label); info[i] = new window.BMap.InfoWindow("<p style=’font-size:12px;lineheight:1.8em;’>" + markerArr[i].title + "</br>地址:" + markerArr[i].address + "</br> 電話:" + markerArr[i].tel + "</br></p>"); // 創建信息窗口對象 } marker[0].addEventListener("mouseover", function () { this.openInfoWindow(info[0]); }); marker[1].addEventListener("mouseover", function () { this.openInfoWindow(info[1]); }); marker[2].addEventListener("mouseover", function () { this.openInfoWindow(info[2]); }); } //異步調用百度js function map_load() { var load = document.createElement("script"); load.src = "http://api.map.baidu.com/api?v=1.4&callback=map_init"; document.body.appendChild(load); } window.onload = map_load; </script> </fieldset> </div> </body> </html>
轉載的
demo.css
body { margin: 0; font-family: "Helvetica,Arial,FreeSans"; color: #000000; font-size: 12px; } .demo_main { padding: 20px; padding-top: 10px; } .demo_title { padding: 10px; margin-bottom: 10px; background-color: #D3D8E0; border: solid 1px gray; } .demo_content { padding: 10px; margin-bottom: 10px; border: solid 1px gray; } fieldset { border: 1px solid gray; }