Google Map API教程之使用GControl對象在地圖上添加control按鈕
來源:程序員人生 發布時間:2013-12-26 08:05:01 閱讀次數:3533次
今天查看我網站的地圖已經被谷歌收錄了5900條,而百度也開始收錄了,很欣慰~再接再厲,今天寫的這一篇Google map api教程是關于在Google地圖上添加control按鈕的,用到的是Google Map API的GControl對象,通過initialize的方法插入一個div到地圖上,再通過new GControlPosition設置按鈕的位置,最后通過Gmap的addControl方法添加這個按鈕。最終效果截圖如下:

實現方法原理分析
首先建立一個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());將這個按鈕加入到地圖中
出處:http://www.js8.in/564.html
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
------分隔線----------------------------
------分隔線----------------------------