百度地图自定义控件分享
废话不多说了,直接奉上代码;
<script>
varmap=newBMap.Map('allmap');
varBcenter=newBMap.Point(116.404,39.915);
map.centerAndZoom(Bcenter,11);
//自定义控件
functionstaticControl(){
this.defaultAnchor=BMAP_ANCHOR_TOP_LEFT;
this.defaultOffset=newBMap.Size(10,10);
}
//继承Control的API
staticControl.prototype=newBMap.Control();
//初始化控件
staticControl.prototype.initialize=function(map){
vardiv=document.createElement('div');
vare1=document.createElement('input');
e1.type='button';
e1.value='重置';
e1.onclick=function(){
statics();
}
div.appendChild(e1);
vare2=document.createElement('input');
e2.type='button';
e2.value='缩小';
e2.onclick=function(){
endStatics();
}
div.appendChild(e2);
vare3=document.createElement("input");
e3.type="button";
e3.value="放大";
e3.onclick=function(){
setStatics();
}
div.appendChild(e3);
//添加DOM元素到地图中
map.getContainer().appendChild(div);
//返回DOM
returndiv;
}
//创建控件实例
varstaticsCtrl=newstaticControl();
//添加到地图当中
map.addControl(staticsCtrl);
functionstatics(){
map.centerAndZoom(newBMap.Point(116.404,39.915),11);
}
functionendStatics(){
map.setZoom(map.getZoom()-2);
}
functionsetStatics(){
map.setZoom(map.getZoom()+2)
}
</script>
上面代码就是个人正在使用的百度地图的自定义控件,希望大家能够喜欢。