打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
HTML5:百度地图手机端单触点单击和长按事件,解决部分手机

HTML5:百度地图手机端单触点单击和长按事件,解决部分手机,有需要的朋友可以参考下。


下面是JS文件


[javascript]view plaincopy
  1. /**
  2. *AuthorYX
  3. *
  4. *对百度地图的事件扩展,目前扩展了fastclick和longclick,
  5. *解决某些设备click不执行的问题
  6. *解决长按事件在拖动、多触点依然执行的bug
  7. *v1.0.0
  8. */

  9. (function(){
  10. BMap.Map.prototype.on=function(evt,fn,data,option){

  11. if(!evt||!fn)return;


  12. var$this=this;
  13. varevtList=["longtouch","onetouch"];
  14. if(inArray(evt,evtList)){
  15. MesureEvents[evt]($this,evt,fn,data,option);
  16. $this.getContainer().querySelector("div.BMap_mask").addEventListener(evt,fn);
  17. }
  18. else{
  19. $this.addEventListener(evt,fn);
  20. }
  21. };

  22. varcenterAndZoom=BMap.Map.prototype.centerAndZoom;

  23. BMap.Map.prototype.centerAndZoom=function(){

  24. var$this=this;
  25. centerAndZoom.apply(this,arguments);
  26. if(!$this.hasRegistMyTouch){
  27. $this.on("onetouch",function(e){
  28. //console.log(e);
  29. varevent=document.createEvent("MouseEvent");
  30. event.initEvent("fastclick",true,true);



  31. event.clientX=e.clientX;
  32. event.clientY=e.clientY;
  33. event.point=e.point;
  34. $this.dispatchEvent(event);

  35. varevent=document.createEvent("MouseEvent");
  36. event.initEvent("click",true,true);
  37. event.clientX=e.clientX;
  38. event.clientY=e.clientY;
  39. $this.dispatchEvent(event);
  40. });
  41. $this.on("longtouch",function(e){
  42. //console.log(e);
  43. varevent=document.createEvent("TouchEvent");
  44. event.initEvent("longclick",true,true);
  45. event.clientX=e.clientX;
  46. event.clientY=e.clientY;
  47. event.point=e.point;
  48. $this.dispatchEvent(event);
  49. });
  50. $this.hasRegistMyTouch=true;
  51. }

  52. }
  53. varMesureEvents={
  54. onetouch:function($this,evt,fn,data,option){
  55. varismoved;
  56. vartime=null;
  57. vartouchLocation=null;
  58. varmaxTouchesCount=0;
  59. $this.addEventListener("touchstart",function(e){
  60. maxTouchesCount=Math.max(maxTouchesCount,e.touches.length);
  61. if(maxTouchesCount==1){
  62. vartouch=e.changedTouches[0];
  63. touchLocation={
  64. x:touch.clientX,
  65. y:touch.clientY
  66. };
  67. time=newDate().getTime();
  68. }
  69. });
  70. $this.addEventListener("touchmove",function(e){
  71. maxTouchesCount=Math.max(maxTouchesCount,e.touches.length);

  72. if(maxTouchesCount==1){
  73. vartouch=e.changedTouches[0];
  74. if(Math.abs(touchLocation.x-touch.clientX)>0&&Math.abs(touchLocation.y-touch.clientY)>0){//解决部分手机对touchmove过分“敏感”的问题
  75. ismoved=true;
  76. //console.log("touchmove---");
  77. }
  78. else{
  79. ismoved=false;
  80. }
  81. }
  82. });
  83. $this.addEventListener("touchend",function(e){

  84. vartouches=e.touches.length;

  85. if(touches==0){
  86. vartemp=maxTouchesCount;
  87. vartempM=ismoved;
  88. ismoved=false;
  89. maxTouchesCount=0;
  90. if(temp==1&&!tempM/*&&/BMap_mask/.test(e.srcElement.className)*/&&newDate().getTime()-time<500){

  91. varevent=document.createEvent("Event");
  92. event.initEvent("onetouch",true,true);
  93. vartouch=e.changedTouches[0];

  94. event.clientX=touch.clientX;
  95. event.clientY=touch.clientY;
  96. event.point=calLngLat($this,event.clientX,event.clientY);
  97. varmask=$this.getContainer().querySelector("div.BMap_mask");
  98. mask.dispatchEvent(event,fn);
  99. }
  100. }
  101. });
  102. },
  103. longtouch:function($this,evt,fn,data,option){
  104. varismoved;
  105. vartime=null;
  106. vartimeout;
  107. varmaxTouchesCount=0;
  108. vartouchLocation=null;
  109. $this.addEventListener("touchstart",function(e){
  110. maxTouchesCount=Math.max(maxTouchesCount,e.touches.length);
  111. if(maxTouchesCount==1){
  112. vartouch=e.changedTouches[0];
  113. //console.log("one:"+touch.clientX+","+touch.clientY);
  114. touchLocation={
  115. x:touch.clientX,
  116. y:touch.clientY
  117. };
  118. time=newDate().getTime();
  119. timeout=setTimeout(function(){

  120. clearTimeout(timeout);
  121. timeout=null;
  122. longtouch(e);
  123. },750);
  124. }
  125. });
  126. $this.addEventListener("touchmove",function(e){
  127. maxTouchesCount=Math.max(maxTouchesCount,e.touches.length);

  128. if(maxTouchesCount==1){
  129. vartouch=e.changedTouches[0];
  130. //console.log("move:"+touch.clientX+","+touch.clientY);
  131. if(Math.abs(touchLocation.x-touch.clientX)>=2&&Math.abs(touchLocation.y-touch.clientY)>2){//解决部分手机对touchmove过分“敏感”的问题
  132. ismoved=true;
  133. //console.log("touchmove---");
  134. if(timeout){
  135. clearTimeout(timeout);
  136. timeout=null;
  137. }
  138. }
  139. else{
  140. ismoved=false;
  141. }
  142. }
  143. else{
  144. if(timeout){
  145. clearTimeout(timeout);
  146. timeout=null;
  147. }
  148. }
  149. });

  150. functionlongtouch(e){
  151. vartemp=maxTouchesCount;
  152. vartempM=ismoved;
  153. ismoved=false;
  154. maxTouchesCount=0;
  155. if(temp==1&&!tempM){

  156. varevent=document.createEvent("Event");
  157. event.initEvent("longtouch",true,true);
  158. vartouch=e.changedTouches[0];

  159. event.clientX=touch.clientX;
  160. event.clientY=touch.clientY;
  161. event.point=calLngLat($this,event.clientX,event.clientY);
  162. $this.getContainer().querySelector("div.BMap_mask").dispatchEvent(event);

  163. }
  164. }

  165. $this.addEventListener("touchend",function(e){

  166. vartouches=e.touches.length;

  167. if(touches==0){
  168. maxTouchesCount=0;
  169. ismoved=false;
  170. }
  171. if(newDate().getTime()-time<1000){
  172. if(timeout){
  173. clearTimeout(timeout);
  174. timeout=null;
  175. }
  176. }
  177. });
  178. }
  179. }

  180. functioncalLngLat($this,x,y){
  181. varcontainer=$this.getContainer();
  182. varrect=container.getBoundingClientRect();
  183. vary=y-rect.top;
  184. varx=x-rect.left;
  185. varbounds=$this.getBounds();
  186. varlefTop=newBMap.Point(bounds.getSouthWest().lng,bounds.getNorthEast().lat);
  187. varlefTopPix=$this.pointToPixel(lefTop);
  188. varpix=newBMap.Pixel(lefTopPix.x+x,lefTopPix.y+y);
  189. varpoint=$this.pixelToPoint(pix);
  190. returnpoint;
  191. }

  192. functioninArray(obj,array){
  193. for(xinarray){
  194. if(obj==array[x])returntrue;
  195. }
  196. returnfalse;
  197. }
  1. })(BMap);

转载博客:http://blog.csdn.net/yyyuuueeee/article/details/49511173
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
如何手 Vue的手势组件呢?本文教你
移动端touch事件获取clientX, clientY
华为T8620手机内存不足怎么办?
HTML5教程:让我们用JS创建一个绘图APP
web前端教程分享javascript 练习题
通过JS 获取Mouse Position(鼠标坐标)的代码
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服