首页 > JavaScript > Google Map API教程之使用GControl对象在地图上添加control按钮

Google Map API教程之使用GControl对象在地图上添加control按钮

今天查看我网站的地图已经被谷歌收录了5900条,而百度也开始收录了,很欣慰~再接再厉,今天写的这一篇Google map api教程是关于在Google地图上添加control按钮的,用到的是Google Map API的GControl对象,通过initialize的方法插入一个div到地图上,再通过new GControlPosition设置按钮的位置,最后通过Gmap的addControl方法添加这个按钮。最终效果截图如下:

control

实现方法原理分析

首先建立一个GControl的原型control,然后定义其initialize 方法:主要是建立一个div,并且制定div的onclick事件,最后通过gmap.getContainer().appendChild(buttonDiv)插入到地图中去。
最后定义getDefaultPosition的方法,通过GControlPosition对象设置按钮所在地图的具体位置。

Google Map API 代码

function control() {};
control.prototype = new GControl();
control.prototype.initialize = function(gmap){
	var buttonDiv = document.createElement("div");
	buttonDiv.id = "control";
	buttonDiv.appendChild(document.createTextNode("清除标点"));
	buttonDiv.onclick = function(){
		gmap.clearOverlays();
	};
	gmap.getContainer().appendChild(buttonDiv);
	return buttonDiv;
};
 
control.prototype.getDefaultPosition = function(){
	return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(510, 7));
};

最后我们再通过gmap.addControl(new control());将这个按钮加入到地图中

全国地图经纬度查询 | 利用GControl对象在地图上添加control按钮实例

相关文章

"Google Map API教程之使用GControl对象在地图上添加control按钮"已有0条评论
  1. 还没有评论呢,赶紧抢沙发!
文明发言,你好我也好
正在提交,请稍后...
返回顶部