地圖覆蓋物
Overlay:覆蓋物的抽象基類,所有的覆蓋物均繼承此類的方法。
Marker:標注表示地圖上的點,可自定義標注的圖標。
Label:表示地圖上的文本標注,您可以自定義標注的文本內容。
Polyline:表示地圖上的折線。
Polygon:表示地圖上的多邊形。多邊形類似於閉合的折線,另外您也可以為其添加填充顏色。
Circle: 表示地圖上的圓。
InfoWindow:信息窗口也是一種特殊的覆蓋物,它可以展示更為豐富的文字和多媒體信息。注意:同一時刻只能有一個信息窗口在地圖上打開
map.addOverlay方法向地圖添加覆蓋物
map.removeOverlay方法移除覆蓋物,注意此方法不適用於InfoWindow
標注
Marker的構造函數的參數為Point和MarkerOptions(可選)。
注意:當您使用自定義圖標時,標注的地理坐標點將位於標注所用圖標的中心位置,您可通過Icon的offset屬性修改標定位置
自定義覆蓋物
API自1.1版本起支持用戶自定義覆蓋物。
要創建自定義覆蓋物,您需要做以下工作:
1.定義一個自定義覆蓋物的構造函數,通過構造函數參數可以傳遞一些自由的變量。
2.設置自定義覆蓋物對象的prototype屬性為Overlay的實例,以便繼承覆蓋物基類。
3.實現initialize方法,當調用map.addOverlay方法時,API會調用此方法。
4.實現draw方法。
基本步驟:
1 var map = new BMap.Map("allmap");//創建地圖實例 2 var point = new BMap.Point(116.404, 39.915);//創建點坐標 3 map.centerAndZoom(point, 15);//地圖初始化,設置中心點坐標和地圖級別。地圖必須經過初始化才可以執行其他操作 4 var marker = new BMap.Marker(point); // 創建標注 5 map.addOverlay(marker); // 將標注添加到地圖中
prototype 屬性來向對象添加屬性
div.style.whiteSpace = "nowrap";段落中的文本不進行換行:
div.style.MozUserSelect = "none";讓文字不被選中
下面是自己做的Demo
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
body, html {width: 100%;height: 100%;margin:0;font-family:"微軟雅黑";}
#allmap{width:100%;height:500px;}
p{margin-left:5px; font-size:14px;}
</style>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=yaDRGoon5YoRzAAwH781yUgn"></script>
<title>添加自定義覆蓋物</title>
</head>
<body>
<div id="allmap"></div>
<div id="control">
<input type="button" onclick="hide_show()" value="點擊">
</div>
</body>
</html>
<script type="text/javascript">
// 百度地圖API功能
var mp = new BMap.Map("allmap");//創建地圖實例
mp.centerAndZoom(new BMap.Point(116.3964,39.9093), 15);
//地圖初始化,設置中心點坐標和地圖級別。地圖必須經過初始化才可以執行其他操作
//啟用鼠標滾輪放大縮小
mp.enableScrollWheelZoom();
//1、定義構造函數並繼承Overlay
//定義自定義覆蓋物的構造函數
function ComplexCustomOverlay(point){
this._point = point;
}
// 繼承API的BMap.Overlay
ComplexCustomOverlay.prototype = new BMap.Overlay();
//2、初始化自定義覆蓋物
// 實現初始化方法
ComplexCustomOverlay.prototype.initialize = function(map){
// 保存map對象實例
this._map = map;
// 創建div元素,作為自定義覆蓋物的容器
var div = this._div = document.createElement("div");
div.style.position = "absolute";
div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);//聚合功能?
// 可以根據參數設置元素外觀
div.style.height = "35px";
div.style.width="35px";
var arrow = this._arrow = document.createElement("img");
arrow.src = "http://www.yantiansf.cn/mapImage/1.gif";
arrow.style.width = "35px";
arrow.style.height = "35px";
arrow.style.top = "22px";
arrow.style.left = "10px";
div.appendChild(arrow);
// 將div添加到覆蓋物容器中
mp.getPanes().labelPane.appendChild(div);//getPanes(),返回值:MapPane,返回地圖覆蓋物容器列表 labelPane呢???
// 需要將div元素作為方法的返回值,當調用該覆蓋物的show、
// hide方法,或者對覆蓋物進行移除時,API都將操作此元素。
return div;
}
//3、繪制覆蓋物
// 實現繪制方法
ComplexCustomOverlay.prototype.draw = function(){
var map = this._map;
var pixel = map.pointToOverlayPixel(this._point);
this._div.style.left = pixel.x - parseInt(this._arrow.style.left) + "px";
this._div.style.top = pixel.y - 30 + "px";
}
//4、自定義覆蓋物添加事件方法
ComplexCustomOverlay.prototype.addEventListener = function(event,fun){
this._div['on'+event] = fun;
}
var myCompOverlay = new ComplexCustomOverlay(new BMap.Point(116.407845,39.914101));
mp.addOverlay(myCompOverlay);//將標注添加到地圖中
//5、 為自定義覆蓋物添加點擊事件
myCompOverlay.addEventListener('click',function(){
alert("點擊圖標");
//hide_show();
});
var show=0;
function hide_show(){
if(show==0){
myCompOverlay.hide();
show=1;
}else{
myCompOverlay.show();
show=0;
}
}
</script>
如果要在覆蓋物上打開信息窗口,那么就只能用marker,因為自定義覆蓋物沒有打開信息窗口的方法,可以用自定義icon,將icon綁定到marker上的方法:
var myIcon = new BMap.Icon("http://www.yantiansf.cn/mapImage/1.gif", new BMap.Size(30,30),{ anchor:new BMap.Size(13,15), imageOffset:new BMap.Size(0,0) }); var mk = new BMap.Marker(point,{icon:myIcon}); // 創建標注
但是這個方法不可以調整圖片大小,所以有可能需要先將圖片調整好大小,然后再使用:
效果如圖(動態的):
測試地址:http://developer.baidu.com/map/jsdemo.htm#c1_16
地圖定位圖標下載:點擊此處
