百度地图(5)-添加标注
这里所说的标注,就是图元对象的意思,也就是向地图上添加各种各样的对象。
所以,标注就牵涉到两个要素:位置和要素,位置知道它在哪里,样式知道它是什么样子。
百度地图主要通过Marker以及Overlay来进行标注的定义和添加。
1. 添加点要素并添加点击事件。
1 function addMarkerPoint() { 2 3 var marker = new BMap.Marker(point); 4 map.addOverlay(marker); 5 6 marker.addEventListener("click", function () { 7 8 var htmlContent = ["<div class='infoBoxContent'><div class='title'><strong>中海雅园</strong><span class='price'>均价43000,overlay-下面的链接无法打开,而infobox可以</span></div>", 9 "<div class='list'><ul><li><div class='left'><img src='/bdMap/img/house3.jpg'/></div><div class='left'><a target='_blank' href='http://map.baidu.com'>中海雅园南北通透四居室</a><p>4室2厅,205.00平米,3层</p></div><div class='rmb'>760万</div></li>" 10 ,"<li><div class='left'><img src='/bdMap/img/house1.jpg'/></div><div class='left'><a target='_blank' href='http://map.baidu.com'>中海雅园四居室还带保姆间</a><p>2室1厅,112.00平米,16层</p></div><div class='rmb'>300万</div></li>" 11 ,"<li><div class='left'><img src='/bdMap/img/house2.jpg'/></div><div class='left'><a target='_blank' href='http://map.baidu.com'>《有钥匙 随时看》花园水系</a><p>3室2厅,241.00平米,16层</p></div><div class='rmb'>400万</div></li>" 12 ,"<li><div class='left'><img src='/bdMap/img/house3.jpg'/></div><div class='left'><a target='_blank' href='http://map.baidu.com'>富力城D区正规楼王大三居</a><p>3室3厅,241.00平米,17层</p></div><div class='rmb'>600万</div></li>" 13 ,"<li class='last'><div class='left'><img src='/bdMap/img/house1.jpg'/></div><div class='left'><a target='_blank' href='http://map.baidu.com'>富力城豪,身份人士的象征</a><p>4室2厅,213.90平米,25层</p></div><div class='rmb'>700万</div></li>" 14 ,"</ul></div>" 15 ,"</div>"]; 16 17 var richContent = new BMapLib.RichMarker(htmlContent.join(""),point,{ 18 19 "anchor": new BMap.Size(-72,-86), 20 "enableDragging":true 21 }); 22 map.addOverlay(richContent); 23 24 }); 25 marker.enableDragging(); 26 marker.addEventListener("dragend", function(e){ 27 alert("当前位置:" + e.point.lng + ", " + e.point.lat); 28 }); 29 }
2. 点标注对象
1 var marker = new BMap.Marker(point); 2 map.addOverlay(marker);
定义了一个 Marker 对象, point为定义的公共变量,默认为地图初始化时的中心点。
然后在地图上通过Overlay添加Marker。这样添加一个默认点标注的功能就完成了。
3. 富文本标注
1 var richContent = new BMapLib.RichMarker(htmlContent.join(""),point,{ 2 3 "anchor": new BMap.Size(-72,-86), 4 "enableDragging":true 5 }); 6 map.addOverlay(richContent);
定义了一个富标注的对象,其包含在BMapLib 中,
引用参考代码:
1 <script type="text/javascript" src="/bdMap/js/BMapLib/RichMarker.min.js"></script>
4. 添加线标注
1 var polyline = new BMap.Polyline([ 2 new BMap.Point(117.2972,31.8988), 3 new BMap.Point(117.2872,31.9088) 4 ], 5 { 6 strokeColor:"blue", 7 strokeWeight:6, 8 strokeOpacity:0.5 9 }); 10 map.addOverlay(polyline);
这里定义了一个Polyline的对象,定义了两个点的坐标,以及填充的颜色、透明度以及线宽度。
5. 添加区域标注
1 var circle = new BMap.Circle(point,500,{ //圆形区域 2 strokeColor:"green", 3 strokeWeight:2, 4 strokeOpacity:0.7 5 }); 6 7 circle.addEventListener("click",function () { 8 var center = circle.getBounds().getCenter(); 9 addSimpleInfo(center,"你点了这个圆。"); 10 }); 11 map.addOverlay(circle); 12 13 var pEnd = new BMap.Point(117.3172,31.9188); // 长方形区域 14 var rectangle = new BMap.Polygon([ 15 new BMap.Point(point.lng,point.lat), 16 new BMap.Point(pEnd.lng,point.lat), 17 new BMap.Point(pEnd.lng,pEnd.lat), 18 new BMap.Point(point.lng,pEnd.lat) 19 ],{ 20 strokeColor:'yellow', 21 strokeWeight:2, 22 strokeOpacity:0.5 23 }); 24 rectangle.addEventListener("click",function () { 25 var center = rectangle.getBounds().getCenter(); 26 addSimpleInfo(center,"你点了这个长方形。"); 27 }); 28 map.addOverlay(rectangle); 29 30 var polygon = new BMap.Polygon([ //多边形区域 31 new BMap.Point(117.2972,31.8988), 32 new BMap.Point(117.2625,31.8635), 33 new BMap.Point(117.3223,31.8534), 34 new BMap.Point(117.3568,31.85863), 35 new BMap.Point(117.3465,31.88641) 36 ],{ 37 strokeColor:"red", 38 strokeWeight: 3, 39 strokeOpacity: 0.6 40 }); 41 polygon.addEventListener("click",function (){ 42 var center = polygon.getBounds().getCenter(); 43 addSimpleInfo(center,"你点了个多边形。") 44 }) 45 map.addOverlay(polygon);
6. 添加TextIcon标注
1 map.addOverlay(new BMapLib.TextIconOverlay(new BMap.Point(117.2972,31.8988), 7)); 2 map.addOverlay(new BMapLib.TextIconOverlay(new BMap.Point(117.2625,31.8635), 15)); 3 map.addOverlay(new BMapLib.TextIconOverlay(new BMap.Point(117.3223,31.8534), 24)); 4 map.addOverlay(new BMapLib.TextIconOverlay(new BMap.Point(117.3568,31.85863), 48)); 5 map.addOverlay(new BMapLib.TextIconOverlay(new BMap.Point(117.3465,31.88641), 99));
TextIcon 类包含在BMapLib 的库中,具体引用参考代码。
7. 添加自定义图标
1 /** 2 * 添加自定义图标标注 3 **/ 4 function addMarkerIcon(){ 5 var myIcon = new BMap.Icon("/bdMap/img/1.gif",new BMap.Size(36,36)); 6 var pt =new BMap.Point(117.3172,31.9188); 7 var marker = new BMap.Marker(pt,{ 8 icon: myIcon 9 }); 10 marker.enableDragging(); 11 map.addOverlay(marker); 12 13 var opts = { 14 position: pt, 15 offset: new BMap.Size(10,10) 16 } 17 var label = new BMap.Label("这是一个测试用的站点",opts); 18 19 marker.addEventListener("onmouseout", function(e) { 20 map.removeOverlay(label); 21 }); 22 marker.addEventListener("onmouseover", function(e) { 23 console.log("泵房站点: " + e.point.lng + " , " + e.point.lat + " ; " + e.pixel.x + " , " + e.pixel.y + " | "); 24 map.addOverlay(label); 25 }); 26 27 addMarkerInfo(marker); 28 }
8. 页面显示
9. 代码参考