偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

百度地圖API如何給自定義覆蓋物添加事件

開(kāi)發(fā) 前端
我們今天要介紹的是百度地圖API自定義覆蓋物的事件應(yīng)該如何添加呢?希望對(duì)大家有所幫助。

  摘要:

  給marker、lable、circle等Overlay添加事件很簡(jiǎn)單,直接addEventListener即可。那么,自定義覆蓋物的事件應(yīng)該如何添加呢?我們一起來(lái)看一看~

  一、定義構(gòu)造函數(shù)并繼承Overlay

  1.   //定義自定義覆蓋物的構(gòu)造函數(shù)  
  2.   functionSquareOverlay(center, length, color){  
  3.   this._center = center;  
  4.   this._length = length;  
  5.   this._color = color;  
  6.   }  
  7.   //繼承API的BMap.Overlay  
  8.   SquareOverlay.prototype = newBMap.Overlay(); 

  二、初始化自定義覆蓋物

  1.   //實(shí)現(xiàn)初始化方法  
  2.   SquareOverlay.prototype.initialize = function(map){  
  3.   //保存map對(duì)象實(shí)例  
  4.   this._map = map;  
  5.   //創(chuàng)建div元素,作為自定義覆蓋物的容器  
  6.   vardiv = document.createElement("div");  
  7.   div.style.position = "absolute";  
  8.   //可以根據(jù)參數(shù)設(shè)置元素外觀  
  9.   div.style.width = this._length + "px";  
  10.   div.style.height = this._length + "px";  
  11.   div.style.background = this._color;  
  12.   //將div添加到覆蓋物容器中  
  13.   map.getPanes().markerPane.appendChild(div);  
  14.  //保存div實(shí)例  
  15.   this._div = div;  
  16.   //需要將div元素作為方法的返回值,當(dāng)調(diào)用該覆蓋物的show、  
  17.   //hide方法,或者對(duì)覆蓋物進(jìn)行移除時(shí),API都將操作此元素。  
  18.   returndiv;  
  19.   } 

  三、繪制覆蓋物

  1.   //實(shí)現(xiàn)繪制方法  
  2.   SquareOverlay.prototype.draw = function(){  
  3.   //根據(jù)地理坐標(biāo)轉(zhuǎn)換為像素坐標(biāo),并設(shè)置給容器  
  4.   varposition = this._map.pointToOverlayPixel(this._center);  
  5.   this._div.style.left = position.x - this._length / 2 + "px";  
  6.   this._div.style.top = position.y - this._length / 2 + "px";  
  7.   } 

  四、添加覆蓋物

  1.   //添加自定義覆蓋物  
  2.   varmySquare = newSquareOverlay(map.getCenter(), 100, "red");  
  3.   map.addOverlay(mySquare); 

  五、給自定義覆蓋物添加事件

  1、顯示事件

  1.   SquareOverlay.prototype.show = function(){  
  2.   if(this._div){  
  3.   this._div.style.display = "";  
  4.   }  
  5.   } 

  添加完以上顯示覆蓋物事件后,只需要下面這句話,就可以顯示覆蓋物了。

  mySquare.show();

  2、隱藏覆蓋物

  1.   //實(shí)現(xiàn)隱藏方法  
  2.   SquareOverlay.prototype.hide = function(){  
  3.   if(this._div){  
  4.   this._div.style.display = "none";  
  5.   }  
  6.   } 

  添加完以上code,只需使用這句話,即可隱藏覆蓋物。

  mySquare.hide();

  3、改變覆蓋物顏色

  1.   SquareOverlay.prototype.yellow = function(){  
  2.   if(this._div){  
  3.   this._div.style.background = "yellow";  
  4.   }  
  5.   } 

  上面這句話,是把覆蓋物的背景顏色改成黃色,使用以下語(yǔ)句即可生效:

  mySquare.yellow();

  “第五部分、給覆蓋物添加事件”小結(jié):

  我們?cè)诘貓D上添加了一個(gè)紅色覆蓋物,然后分別添加“顯示、隱藏、改變顏色”的事件。示意圖如下:

那么,我們需要在html里,先寫(xiě)出map的容器,和3個(gè)按鈕。

  1.   <div style="width:520px;height:340px;border:1px solid gray"id="container"></div> 
  2.   <p> 
  3.   <input type="button"value="移除覆蓋物"onclick="mySquare.hide();"/> 
  4.   <input type="button"value="顯示覆蓋物"onclick="mySquare.show();"/> 
  5.   <input type="button"value="變成黃色"onclick="mySquare.yellow();"/> 
  6.   </p> 
  7.   然后,在javascript中,添加這三個(gè)函數(shù):  
  8.   //實(shí)現(xiàn)顯示方法  
  9.   SquareOverlay.prototype.show = function(){  
  10.   if(this._div){  
  11.   this._div.style.display = "";  
  12.   }  
  13.   }  
  14.   //實(shí)現(xiàn)隱藏方法  
  15.   SquareOverlay.prototype.hide = function(){  
  16.   if(this._div){  
  17.   this._div.style.display = "none";  
  18.   }  
  19.   }  
  20.   //改變顏色的方法  
  21.   SquareOverlay.prototype.yellow = function(){  
  22.  if(this._div){  
  23.   this._div.style.background = "yellow";  
  24.   }  
  25.   } 

  六、如何給自定義覆蓋物添加點(diǎn)擊事件(這章重要!很多人問(wèn)的)

  比如,我們給自定義覆蓋物點(diǎn)擊click事件。首先,需要添加一個(gè)addEventListener 的事件。如下:

  1.   SquareOverlay.prototype.addEventListener = function(event,fun){  
  2.   this._div['on'+event] = fun;  
  3.   } 

  再寫(xiě)該函數(shù)里面的參數(shù),比如click。這樣就跟百度地圖API里面的覆蓋物事件一樣了。

  1.   mySquare.addEventListener('click',function(){  
  2.   alert('click');  
  3.   }); 

  同理,添加完畢addEventListener之后,還可以添加其他鼠標(biāo)事件,比如mouseover。

  mySquare.addEventListener('mousemover',function(){

  alert('鼠標(biāo)移上來(lái)了');

  });

  七、全部源代碼

自定義覆蓋物

  1. <!DOCTYPE html> 
  2. <html> 
  3. <head> 
  4. <meta http-equiv="Content-Type"content="text/html; charset=utf-8"/> 
  5. <title>自定義覆蓋物的點(diǎn)擊事件</title> 
  6. <script type="text/javascript"src="http://api.map.baidu.com/api?v=1.2"></script> 
  7. </head> 
  8. <body> 
  9. <div style="width:520px;height:340px;border:1px solid gray"id="container"></div> 
  10. <p> 
  11. <input type="button"value="移除覆蓋物"onclick="mySquare.hide();"/> 
  12. <input type="button"value="顯示覆蓋物"onclick="mySquare.show();"/> 
  13. <input type="button"value="變成黃色"onclick="mySquare.yellow();"/> 
  14. </p> 
  15. </body> 
  16. </html> 
  17. <script type="text/javascript"> 
  18. varmap =newBMap.Map("container"); //創(chuàng)建Map實(shí)例  
  19. varpoint =newBMap.Point(116.404, 39.915); //創(chuàng)建點(diǎn)坐標(biāo)  
  20. map.centerAndZoom(point,15); //初始化地圖,設(shè)置中心點(diǎn)坐標(biāo)和地圖級(jí)別。  
  21. //1、定義構(gòu)造函數(shù)并繼承Overlay  
  22. //定義自定義覆蓋物的構(gòu)造函數(shù)  
  23. functionSquareOverlay(center, length, color){ 25this._center =center; 26this._length =length; 27this._color =color; 28} 29//繼承API的BMap.Overlay  
  24. SquareOverlay.prototype =newBMap.Overlay();   
  25. //2、初始化自定義覆蓋物  
  26. //實(shí)現(xiàn)初始化方法  
  27. SquareOverlay.prototype.initialize =function(map){   
  28. //保存map對(duì)象實(shí)例  
  29. this._map =map;   
  30. //創(chuàng)建div元素,作為自定義覆蓋物的容器  
  31. vardiv =document.createElement("div"); div.style.position ="absolute";  
  32. //可以根據(jù)參數(shù)設(shè)置元素外觀  
  33. div.style.width =this._length +"px";  
  34. div.style.height =this._length +"px";  
  35. div.style.background =this._color;   
  36. //將div添加到覆蓋物容器中  
  37. map.getPanes().markerPane.appendChild(div); 46//保存div實(shí)例  
  38. this._div =div; 48//需要將div元素作為方法的返回值,當(dāng)調(diào)用該覆蓋物的show、  
  39. //hide方法,或者對(duì)覆蓋物進(jìn)行移除時(shí),API都將操作此元素。  
  40. returndiv;   
  41. }  
  42. //3、繪制覆蓋物  
  43. //實(shí)現(xiàn)繪制方法  
  44. SquareOverlay.prototype.draw =function(){   
  45. //根據(jù)地理坐標(biāo)轉(zhuǎn)換為像素坐標(biāo),并設(shè)置給容器  
  46. varposition =this._map.pointToOverlayPixel(this._center);  
  47. this._div.style.left =position.x -this._length /2+"px";  
  48. this._div.style.top =position.y -this._length /2+"px";  
  49. }  
  50. //4、顯示和隱藏覆蓋物  
  51. //實(shí)現(xiàn)顯示方法  
  52. SquareOverlay.prototype.show =function(){   
  53. if(this._div){   
  54. this._div.style.display ="";  
  55. }   
  56. }   
  57. //實(shí)現(xiàn)隱藏方法  
  58. SquareOverlay.prototype.hide =function(){   
  59. if(this._div){   
  60. this._div.style.display ="none";  
  61. }   
  62. }  
  63. //5、添加其他覆蓋物方法  
  64. //比如,改變顏色  
  65. SquareOverlay.prototype.yellow =function(){   
  66. if(this._div){   
  67. this._div.style.background ="yellow";}   
  68. }  
  69. //6、自定義覆蓋物添加事件方法  
  70. SquareOverlay.prototype.addEventListener =function(event,fun){  
  71. this._div['on'+event] =fun;  
  72. }  
  73. //7、添加自定義覆蓋物  
  74. varmySquare =newSquareOverlay(map.getCenter(), 100, "red"); 91map.addOverlay(mySquare);  
  75. //8、 為自定義覆蓋物添加點(diǎn)擊事件  
  76. mySquare.addEventListener('click',function(){  
  77. alert('click');  
  78. });  
  79. </script> 

  八、感謝大家支持!

原文鏈接:http://www.cnblogs.com/milkmap/archive/2011/10/20/2219149.html

【編輯推薦】

  1. 詳解百度地圖API之地圖標(biāo)注
  2. 百度地圖API之如何制作駕車導(dǎo)航
  3. 詳解百度地圖API之地圖操作
  4. 詳解百度地圖API之自定義地圖類型
  5. 怎么成為一個(gè)軟件架構(gòu)師

 

責(zé)任編輯:彭凡 來(lái)源: 博客園
相關(guān)推薦

2011-10-09 11:07:40

百度地圖API

2011-10-21 09:28:25

百度地圖API

2011-09-29 11:00:54

百度地圖API

2011-10-21 10:16:25

百度地圖API

2011-10-24 14:01:29

API

2011-09-16 14:39:02

百度地圖API

2011-09-16 10:37:42

地圖API

2011-09-26 10:05:19

百度地圖API

2012-02-01 09:33:36

百度地圖API

2021-06-15 14:33:00

高德百度騰訊

2022-03-27 10:04:23

Angular8項(xiàng)目vue

2010-01-28 10:29:44

2013-04-08 14:59:54

Android學(xué)習(xí)筆記百度地圖Overlay

2011-12-29 16:18:14

API

2009-12-14 20:05:05

內(nèi)容

2014-07-25 17:12:39

數(shù)據(jù)庫(kù)WOT2014MongoDB

2013-04-08 14:46:42

Android學(xué)習(xí)筆記百度地圖

2011-05-25 14:36:17

2012-02-03 14:01:15

地圖

2013-08-22 17:08:50

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)