成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

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

  摘要:

  給marker、lable、circle等Overlay添加事件很簡單,直接addEventListener即可。那么,自定義覆蓋物的事件應該如何添加呢?我們一起來看一看~

  一、定義構造函數并繼承Overlay

  1.   //定義自定義覆蓋物的構造函數  
  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.   //實現初始化方法  
  2.   SquareOverlay.prototype.initialize = function(map){  
  3.   //保存map對象實例  
  4.   this._map = map;  
  5.   //創建div元素,作為自定義覆蓋物的容器  
  6.   vardiv = document.createElement("div");  
  7.   div.style.position = "absolute";  
  8.   //可以根據參數設置元素外觀  
  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實例  
  15.   this._div = div;  
  16.   //需要將div元素作為方法的返回值,當調用該覆蓋物的show、  
  17.   //hide方法,或者對覆蓋物進行移除時,API都將操作此元素。  
  18.   returndiv;  
  19.   } 

  三、繪制覆蓋物

  1.   //實現繪制方法  
  2.   SquareOverlay.prototype.draw = function(){  
  3.   //根據地理坐標轉換為像素坐標,并設置給容器  
  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.   //實現隱藏方法  
  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.   } 

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

  mySquare.yellow();

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

  我們在地圖上添加了一個紅色覆蓋物,然后分別添加“顯示、隱藏、改變顏色”的事件。示意圖如下:

那么,我們需要在html里,先寫出map的容器,和3個按鈕。

  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中,添加這三個函數:  
  8.   //實現顯示方法  
  9.   SquareOverlay.prototype.show = function(){  
  10.   if(this._div){  
  11.   this._div.style.display = "";  
  12.   }  
  13.   }  
  14.   //實現隱藏方法  
  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.   } 

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

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

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

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

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

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

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

  alert('鼠標移上來了');

  });

  七、全部源代碼

自定義覆蓋物

  1. <!DOCTYPE html> 
  2. <html> 
  3. <head> 
  4. <meta http-equiv="Content-Type"content="text/html; charset=utf-8"/> 
  5. <title>自定義覆蓋物的點擊事件</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"); //創建Map實例  
  19. varpoint =newBMap.Point(116.404, 39.915); //創建點坐標  
  20. map.centerAndZoom(point,15); //初始化地圖,設置中心點坐標和地圖級別。  
  21. //1、定義構造函數并繼承Overlay  
  22. //定義自定義覆蓋物的構造函數  
  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. //實現初始化方法  
  27. SquareOverlay.prototype.initialize =function(map){   
  28. //保存map對象實例  
  29. this._map =map;   
  30. //創建div元素,作為自定義覆蓋物的容器  
  31. vardiv =document.createElement("div"); div.style.position ="absolute";  
  32. //可以根據參數設置元素外觀  
  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實例  
  38. this._div =div; 48//需要將div元素作為方法的返回值,當調用該覆蓋物的show、  
  39. //hide方法,或者對覆蓋物進行移除時,API都將操作此元素。  
  40. returndiv;   
  41. }  
  42. //3、繪制覆蓋物  
  43. //實現繪制方法  
  44. SquareOverlay.prototype.draw =function(){   
  45. //根據地理坐標轉換為像素坐標,并設置給容器  
  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. //實現顯示方法  
  52. SquareOverlay.prototype.show =function(){   
  53. if(this._div){   
  54. this._div.style.display ="";  
  55. }   
  56. }   
  57. //實現隱藏方法  
  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、 為自定義覆蓋物添加點擊事件  
  76. mySquare.addEventListener('click',function(){  
  77. alert('click');  
  78. });  
  79. </script> 

  八、感謝大家支持!

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

【編輯推薦】

  1. 詳解百度地圖API之地圖標注
  2. 百度地圖API之如何制作駕車導航
  3. 詳解百度地圖API之地圖操作
  4. 詳解百度地圖API之自定義地圖類型
  5. 怎么成為一個軟件架構師

 

責任編輯:彭凡 來源: 博客園
相關推薦

2011-10-09 11:07:40

百度地圖API

2011-10-21 09:28:25

百度地圖API

2011-09-29 11:00:54

百度地圖API

2011-09-16 14:39:02

百度地圖API

2011-10-24 14:01:29

API

2011-10-21 10:16:25

百度地圖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項目vue

2010-01-28 10:29:44

2013-04-08 14:59:54

Android學習筆記百度地圖Overlay

2011-12-29 16:18:14

API

2009-12-14 20:05:05

內容

2013-04-08 14:46:42

Android學習筆記百度地圖

2014-07-25 17:12:39

數據庫WOT2014MongoDB

2011-05-25 14:36:17

2012-02-03 14:01:15

地圖

2013-08-22 17:08:50

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 精品不卡 | 一级全黄少妇性色生活免费看 | 欧美伊人久久久久久久久影院 | 成人免费观看视频 | 欧美在线免费 | 精品一级 | 91成人午夜性a一级毛片 | 亚洲丝袜天堂 | 神马影院一区二区三区 | www.亚洲视频| 久草99 | 九色网址 | 一区二区三区观看视频 | 最新国产精品视频 | 欧美毛片免费观看 | 成人在线精品视频 | 久久夜夜 | 中文字幕一页二页 | 亚洲视频免费在线 | 丁香婷婷久久久综合精品国产 | www久久久 | 国内自拍视频在线观看 | 欧美成人综合 | 91在线一区 | 国产乱一区二区三区视频 | 久久婷婷香蕉热狠狠综合 | 精品日韩一区 | 成人欧美日韩一区二区三区 | 日韩视频精品在线 | 国产精品视频网 | 亚洲精品国产区 | 99福利视频导航 | 一区二区三区四区在线 | 日韩免费一二三区 | 日韩av成人 | 亚洲一区二区三区免费 | 日韩免费一二三区 | 国产一区二区免费电影 | 日本三级日产三级国产三级 | 日韩精品在线视频免费观看 | 精品国产乱码一区二区三区a |