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.打开浏览器