1.定義一個控件類,即function
function ZoomControl(){ // 設置默認停靠位置和偏移量 this.defaultAnchor = BMAP_ANCHOR_TOP_LEFT; this.defaultOffset = new BMap.Size(10, 10); }
2.通過JavaScript的prototype屬性繼承于BMap.Control?
ZoomControl.prototype = new BMap.Control();
?
3.自定義控件必須實現自己的initialize方法,并且將控件的DOM元素返回
ZoomControl.prototype.initialize = function(map){var b1 = document.createElement("input");b1.type = "button";b1.value="放大1級";b1.onclick = function(e){ map.zoomTo(map.getZoom() + 1); }var b2 = document.createElement("input");b2.type = "button";b2.value="縮小1級";b2.onclick = function(e){ map.zoomTo(map.getZoom() - 1); }var div = document.createElement("div"); div.appendChild(b1); div.appendChild(b2);map.getContainer().appendChild(div); return div; }
?
?
4.創建控件,?添加到地圖當中
var myZoomCtrl = new ZoomControl(); map.addControl(myZoomCtrl);
5.打開瀏覽器