百度地图显示多个标注点

效果图:


[html]   view plain  copy  print ?
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <title>百度地图API显示多个标注点带提示的代码</title>  
  5.     <!--css-->  
  6.     <link href="style/demo.css" rel="stylesheet" type="text/css" />  
  7.     <!--javascript-->  
  8.     <script src="scripts/jquery-1.9.1.js" type="text/javascript"></script>  
  9.     <script src="scripts/demo.js" type="text/javascript"></script>  
  10. </head>  
  11. <body>  
  12.     <div class="demo_main">  
  13.         <fieldset class="demo_title">  
  14.             百度地图API显示多个标注点带提示的代码  
  15.         </fieldset>  
  16.         <fieldset class="demo_content">  
  17.             <div style="min-height: 300px; width: 100%;" id="map">  
  18.             </div>  
  19.             <script type="text/javascript">  
  20.                 var markerArr = [  
  21.                     { title: "名称:广州火车站", point: "113.264531,23.157003", address: "广东省广州市广州火车站", tel: "12306" },  
  22.                     { title: "名称:广州塔(赤岗塔)", point: "113.330934,23.113401", address: "广东省广州市广州塔(赤岗塔) ", tel: "18500000000" },  
  23.                     { title: "名称:广州动物园", point: "113.312213,23.147267", address: "广东省广州市广州动物园", tel: "18500000000" },  
  24.                     { title: "名称:天河公园", point: "113.372867,23.134274", address: "广东省广州市天河公园", tel: "18500000000" }  
  25.   
  26.                 ];  
  27.   
  28.                 function map_init() {  
  29.                     var map = new BMap.Map("map"); // 创建Map实例  
  30.                     var point = new BMap.Point(113.312213, 23.147267); //地图中心点,广州市  
  31.                     map.centerAndZoom(point, 13); // 初始化地图,设置中心点坐标和地图级别。  
  32.                     map.enableScrollWheelZoom(true); //启用滚轮放大缩小  
  33.                     //向地图中添加缩放控件  
  34.                     var ctrlNav = new window.BMap.NavigationControl({  
  35.                         anchor: BMAP_ANCHOR_TOP_LEFT,  
  36.                         type: BMAP_NAVIGATION_CONTROL_LARGE  
  37.                     });  
  38.                     map.addControl(ctrlNav);  
  39.   
  40.                     //向地图中添加缩略图控件  
  41.                     var ctrlOve = new window.BMap.OverviewMapControl({  
  42.                         anchor: BMAP_ANCHOR_BOTTOM_RIGHT,  
  43.                         isOpen: 1  
  44.                     });  
  45.                     map.addControl(ctrlOve);  
  46.   
  47.                     //向地图中添加比例尺控件  
  48.                     var ctrlSca = new window.BMap.ScaleControl({  
  49.                         anchor: BMAP_ANCHOR_BOTTOM_LEFT  
  50.                     });  
  51.                     map.addControl(ctrlSca);  
  52.   
  53.                     var point = new Array(); //存放标注点经纬信息的数组  
  54.                     var marker = new Array(); //存放标注点对象的数组  
  55.                     var info = new Array(); //存放提示信息窗口对象的数组  
  56.                     for (var i = 0; i < markerArr.length; i++) {  
  57.                         var p0 = markerArr[i].point.split(",")[0]; //  
  58.                         var p1 = markerArr[i].point.split(",")[1]; //按照原数组的point格式将地图点坐标的经纬度分别提出来  
  59.                         point[i] = new window.BMap.Point(p0, p1); //循环生成新的地图点  
  60.                         marker[i] = new window.BMap.Marker(point[i]); //按照地图点坐标生成标记  
  61.                         map.addOverlay(marker[i]);  
  62.                         marker[i].setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画  
  63.                         var label = new window.BMap.Label(markerArr[i].title, { offset: new window.BMap.Size(20, -10) });  
  64.                         marker[i].setLabel(label);  
  65.                         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>"); // 创建信息窗口对象  
  66.                     }  
  67.                     marker[0].addEventListener("mouseover", function () {  
  68.                         this.openInfoWindow(info[0]);  
  69.                     });  
  70.                     marker[1].addEventListener("mouseover", function () {  
  71.                         this.openInfoWindow(info[1]);  
  72.                     });  
  73.                     marker[2].addEventListener("mouseover", function () {  
  74.                         this.openInfoWindow(info[2]);  
  75.                     });  
  76.                 }  
  77.                 //异步调用百度js  
  78.                 function map_load() {  
  79.                     var load = document.createElement("script");  
  80.                     load.src = "http://api.map.baidu.com/api?v=1.4&callback=map_init";  
  81.                     document.body.appendChild(load);  
  82.                 }  
  83.                 window.onload = map_load;  
  84.             </script>  
  85.         </fieldset>  
  86.     </div>  
  87. </body>  
  88. </html>  

完整demo下载

2015-6-28 补充:动态添加标记提示:(将以下代码单独保存为html文件,打开即可预览效果)

[html]   view plain  copy  print ?
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <title>百度地图API显示多个标注点带提示的代码</title>  
  5.     <!--css-->  
  6.     <style type="text/css">  
  7.         body { margin: 0; font-family: "Helvetica,Arial,FreeSans"; color: #000000; font-size: 12px; }  
  8.         .demo_main { padding: 20px; padding-top: 10px; }  
  9.         .demo_title { padding: 10px; margin-bottom: 10px; background-color: #D3D8E0; border: solid 1px gray; }  
  10.         .demo_content { padding: 10px; margin-bottom: 10px; border: solid 1px gray; }  
  11.         fieldset { border: 1px solid gray; }  
  12.     </style>  
  13.     <!--javascript-->  
  14.     <script src="http://www.w3school.com.cn/jquery/jquery.js" type="text/javascript"></script>  
  15. </head>  
  16. <body>  
  17.     <div class="demo_main">  
  18.         <fieldset class="demo_title">  
  19.             百度地图API显示多个标注点带提示的代码  
  20.         </fieldset>  
  21.         <fieldset class="demo_content">  
  22.             <div style="min-height: 300px; width: 100%;" id="map">  
  23.             </div>  
  24.             <script type="text/javascript">  
  25.                 var markerArr = [  
  26.                     { title: "名称:广州火车站", point: "113.264531,23.157003", address: "广东省广州市广州火车站", tel: "12306" },  
  27.                     { title: "名称:广州塔(赤岗塔)", point: "113.330934,23.113401", address: "广东省广州市广州塔(赤岗塔) ", tel: "18500000000" },  
  28.                     { title: "名称:广州动物园", point: "113.312213,23.147267", address: "广东省广州市广州动物园", tel: "18500000000" },  
  29.                     { title: "名称:天河公园", point: "113.372867,23.134274", address: "广东省广州市天河公园", tel: "18500000000" }  
  30.                 ];  
  31.   
  32.                 var map; //Map实例  
  33.                 function map_init() {  
  34.                     map = new BMap.Map("map");  
  35.                     //第1步:设置地图中心点,广州市  
  36.                     var point = new BMap.Point(113.312213, 23.147267);  
  37.                     //第2步:初始化地图,设置中心点坐标和地图级别。  
  38.                     map.centerAndZoom(point, 13);  
  39.                     //第3步:启用滚轮放大缩小  
  40.                     map.enableScrollWheelZoom(true);  
  41.                     //第4步:向地图中添加缩放控件  
  42.                     var ctrlNav = new window.BMap.NavigationControl({  
  43.                         anchor: BMAP_ANCHOR_TOP_LEFT,  
  44.                         type: BMAP_NAVIGATION_CONTROL_LARGE  
  45.                     });  
  46.                     map.addControl(ctrlNav);  
  47.                     //第5步:向地图中添加缩略图控件  
  48.                     var ctrlOve = new window.BMap.OverviewMapControl({  
  49.                         anchor: BMAP_ANCHOR_BOTTOM_RIGHT,  
  50.                         isOpen: 1  
  51.                     });  
  52.                     map.addControl(ctrlOve);  
  53.   
  54.                     //第6步:向地图中添加比例尺控件  
  55.                     var ctrlSca = new window.BMap.ScaleControl({  
  56.                         anchor: BMAP_ANCHOR_BOTTOM_LEFT  
  57.                     });  
  58.                     map.addControl(ctrlSca);  
  59.   
  60.                     //第7步:绘制点    
  61.                     for (var i = 0; i < markerArr.length; i++) {  
  62.                         var p0 = markerArr[i].point.split(",")[0];  
  63.                         var p1 = markerArr[i].point.split(",")[1];  
  64.                         var maker = addMarker(new window.BMap.Point(p0, p1), i);  
  65.                         addInfoWindow(maker, markerArr[i], i);   
  66.                     }  
  67.                 }  
  68.   
  69.                 // 添加标注  
  70.                 function addMarker(point, index) {  
  71.                     var myIcon = new BMap.Icon("http://api.map.baidu.com/img/markers.png",  
  72.                         new BMap.Size(23, 25), {  
  73.                             offset: new BMap.Size(10, 25),  
  74.                             imageOffset: new BMap.Size(0, 0 - index * 25)  
  75.                         });  
  76.                     var marker = new BMap.Marker(point, { icon: myIcon });  
  77.                     map.addOverlay(marker);  
  78.                     return marker;  
  79.                 }  
  80.   
  81.                 // 添加信息窗口  
  82.                 function addInfoWindow(marker, poi) {  
  83.                     //pop弹窗标题  
  84.                     var title = '<div style="font-weight:bold;color:#CE5521;font-size:14px">' + poi.title + '</div>';  
  85.                     //pop弹窗信息  
  86.                     var html = [];  
  87.                     html.push('<table cellspacing="0" style="table-layout:fixed;width:100%;font:12px arial,simsun,sans-serif"><tbody>');  
  88.                     html.push('<tr>');  
  89.                     html.push('<td style="vertical-align:top;line-height:16px;width:38px;white-space:nowrap;word-break:keep-all">地址:</td>');  
  90.                     html.push('<td style="vertical-align:top;line-height:16px">' + poi.address + ' </td>');  
  91.                     html.push('</tr>');  
  92.                     html.push('</tbody></table>');  
  93.                     var infoWindow = new BMap.InfoWindow(html.join(""), { title: title, width: 200 });  
  94.   
  95.                     var openInfoWinFun = function () {  
  96.                         marker.openInfoWindow(infoWindow);  
  97.                     };  
  98.                     marker.addEventListener("click", openInfoWinFun);  
  99.                     return openInfoWinFun;  
  100.                 }  
  101.   
  102.                 //异步调用百度js  
  103.                 function map_load() {  
  104.                     var load = document.createElement("script");  
  105.                     load.src = "http://api.map.baidu.com/api?v=1.4&callback=map_init";  
  106.                     document.body.appendChild(load);  
  107.                 }  
  108.                 window.onload = map_load;  
  109.             </script>  
  110.         </fieldset>  
  111.     </div>  
  112. </body>  
  113. </html>  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值