百度地圖API如何給自定義覆蓋物添加事件
摘要:
給marker、lable、circle等Overlay添加事件很簡單,直接addEventListener即可。那么,自定義覆蓋物的事件應該如何添加呢?我們一起來看一看~
一、定義構造函數并繼承Overlay
- //定義自定義覆蓋物的構造函數
- functionSquareOverlay(center, length, color){
- this._center = center;
- this._length = length;
- this._color = color;
- }
- //繼承API的BMap.Overlay
- SquareOverlay.prototype = newBMap.Overlay();
二、初始化自定義覆蓋物
- //實現初始化方法
- SquareOverlay.prototype.initialize = function(map){
- //保存map對象實例
- this._map = map;
- //創建div元素,作為自定義覆蓋物的容器
- vardiv = document.createElement("div");
- div.style.position = "absolute";
- //可以根據參數設置元素外觀
- div.style.width = this._length + "px";
- div.style.height = this._length + "px";
- div.style.background = this._color;
- //將div添加到覆蓋物容器中
- map.getPanes().markerPane.appendChild(div);
- //保存div實例
- this._div = div;
- //需要將div元素作為方法的返回值,當調用該覆蓋物的show、
- //hide方法,或者對覆蓋物進行移除時,API都將操作此元素。
- returndiv;
- }
三、繪制覆蓋物
- //實現繪制方法
- SquareOverlay.prototype.draw = function(){
- //根據地理坐標轉換為像素坐標,并設置給容器
- varposition = this._map.pointToOverlayPixel(this._center);
- this._div.style.left = position.x - this._length / 2 + "px";
- this._div.style.top = position.y - this._length / 2 + "px";
- }
四、添加覆蓋物
- //添加自定義覆蓋物
- varmySquare = newSquareOverlay(map.getCenter(), 100, "red");
- map.addOverlay(mySquare);
五、給自定義覆蓋物添加事件
1、顯示事件
- SquareOverlay.prototype.show = function(){
- if(this._div){
- this._div.style.display = "";
- }
- }
添加完以上顯示覆蓋物事件后,只需要下面這句話,就可以顯示覆蓋物了。
mySquare.show();
2、隱藏覆蓋物
- //實現隱藏方法
- SquareOverlay.prototype.hide = function(){
- if(this._div){
- this._div.style.display = "none";
- }
- }
添加完以上code,只需使用這句話,即可隱藏覆蓋物。
mySquare.hide();
3、改變覆蓋物顏色
- SquareOverlay.prototype.yellow = function(){
- if(this._div){
- this._div.style.background = "yellow";
- }
- }
上面這句話,是把覆蓋物的背景顏色改成黃色,使用以下語句即可生效:
mySquare.yellow();
“第五部分、給覆蓋物添加事件”小結:
我們在地圖上添加了一個紅色覆蓋物,然后分別添加“顯示、隱藏、改變顏色”的事件。示意圖如下:
那么,我們需要在html里,先寫出map的容器,和3個按鈕。
- <div style="width:520px;height:340px;border:1px solid gray"id="container"></div>
- <p>
- <input type="button"value="移除覆蓋物"onclick="mySquare.hide();"/>
- <input type="button"value="顯示覆蓋物"onclick="mySquare.show();"/>
- <input type="button"value="變成黃色"onclick="mySquare.yellow();"/>
- </p>
- 然后,在javascript中,添加這三個函數:
- //實現顯示方法
- SquareOverlay.prototype.show = function(){
- if(this._div){
- this._div.style.display = "";
- }
- }
- //實現隱藏方法
- SquareOverlay.prototype.hide = function(){
- if(this._div){
- this._div.style.display = "none";
- }
- }
- //改變顏色的方法
- SquareOverlay.prototype.yellow = function(){
- if(this._div){
- this._div.style.background = "yellow";
- }
- }
六、如何給自定義覆蓋物添加點擊事件(這章重要!很多人問的)
比如,我們給自定義覆蓋物點擊click事件。首先,需要添加一個addEventListener 的事件。如下:
- SquareOverlay.prototype.addEventListener = function(event,fun){
- this._div['on'+event] = fun;
- }
再寫該函數里面的參數,比如click。這樣就跟百度地圖API里面的覆蓋物事件一樣了。
- mySquare.addEventListener('click',function(){
- alert('click');
- });
同理,添加完畢addEventListener之后,還可以添加其他鼠標事件,比如mouseover。
mySquare.addEventListener('mousemover',function(){
alert('鼠標移上來了');
});
七、全部源代碼
自定義覆蓋物
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type"content="text/html; charset=utf-8"/>
- <title>自定義覆蓋物的點擊事件</title>
- <script type="text/javascript"src="http://api.map.baidu.com/api?v=1.2"></script>
- </head>
- <body>
- <div style="width:520px;height:340px;border:1px solid gray"id="container"></div>
- <p>
- <input type="button"value="移除覆蓋物"onclick="mySquare.hide();"/>
- <input type="button"value="顯示覆蓋物"onclick="mySquare.show();"/>
- <input type="button"value="變成黃色"onclick="mySquare.yellow();"/>
- </p>
- </body>
- </html>
- <script type="text/javascript">
- varmap =newBMap.Map("container"); //創建Map實例
- varpoint =newBMap.Point(116.404, 39.915); //創建點坐標
- map.centerAndZoom(point,15); //初始化地圖,設置中心點坐標和地圖級別。
- //1、定義構造函數并繼承Overlay
- //定義自定義覆蓋物的構造函數
- functionSquareOverlay(center, length, color){ 25this._center =center; 26this._length =length; 27this._color =color; 28} 29//繼承API的BMap.Overlay
- SquareOverlay.prototype =newBMap.Overlay();
- //2、初始化自定義覆蓋物
- //實現初始化方法
- SquareOverlay.prototype.initialize =function(map){
- //保存map對象實例
- this._map =map;
- //創建div元素,作為自定義覆蓋物的容器
- vardiv =document.createElement("div"); div.style.position ="absolute";
- //可以根據參數設置元素外觀
- div.style.width =this._length +"px";
- div.style.height =this._length +"px";
- div.style.background =this._color;
- //將div添加到覆蓋物容器中
- map.getPanes().markerPane.appendChild(div); 46//保存div實例
- this._div =div; 48//需要將div元素作為方法的返回值,當調用該覆蓋物的show、
- //hide方法,或者對覆蓋物進行移除時,API都將操作此元素。
- returndiv;
- }
- //3、繪制覆蓋物
- //實現繪制方法
- SquareOverlay.prototype.draw =function(){
- //根據地理坐標轉換為像素坐標,并設置給容器
- varposition =this._map.pointToOverlayPixel(this._center);
- this._div.style.left =position.x -this._length /2+"px";
- this._div.style.top =position.y -this._length /2+"px";
- }
- //4、顯示和隱藏覆蓋物
- //實現顯示方法
- SquareOverlay.prototype.show =function(){
- if(this._div){
- this._div.style.display ="";
- }
- }
- //實現隱藏方法
- SquareOverlay.prototype.hide =function(){
- if(this._div){
- this._div.style.display ="none";
- }
- }
- //5、添加其他覆蓋物方法
- //比如,改變顏色
- SquareOverlay.prototype.yellow =function(){
- if(this._div){
- this._div.style.background ="yellow";}
- }
- //6、自定義覆蓋物添加事件方法
- SquareOverlay.prototype.addEventListener =function(event,fun){
- this._div['on'+event] =fun;
- }
- //7、添加自定義覆蓋物
- varmySquare =newSquareOverlay(map.getCenter(), 100, "red"); 91map.addOverlay(mySquare);
- //8、 為自定義覆蓋物添加點擊事件
- mySquare.addEventListener('click',function(){
- alert('click');
- });
- </script>
八、感謝大家支持!
原文鏈接:http://www.cnblogs.com/milkmap/archive/2011/10/20/2219149.html
【編輯推薦】