博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
解决百度地图移动端(微信浏览器等)拖拽事件和点击事件冲突的BUG
阅读量:2021 次
发布时间:2019-04-28

本文共 11066 字,大约阅读时间需要 36 分钟。

在手机端,如果启用了百度地图的拖拽,那么部分手机上点击事件无法触发,无法获取点击位置的坐标,除非禁用拖拽的功能。

但如果禁用拖拽后,用户体验就很差,所以以下代码可以直接解决问题,亲测可用,如有疑问可以私信或者留言。

直接上代码

把代码复制到引用百度JS的后面,然后把自己的click事件改为fastclick即可!

/**  * Author 岳晓  *   * 对百度地图的事件扩展,目前扩展了fastclick和longclick,  * 解决某些设备click不执行的问题  * 解决长按事件在拖动、多触点依然执行的bug  * v1.0.0  */    (function(){      BMap.Map.prototype.on=function(evt,fn,option){          var _option = {              canBubble:true          }                    extend(_option,option)                    if(!evt || !fn) return;                              var $this = this;          var evtList = ["longtouch","onetouch"];          if(inArray(evt,evtList)){              MesureEvents[evt]($this,evt,fn,option);              $this.getContainer().querySelector("div.BMap_mask").addEventListener(evt,fn);          }          else{              $this.addEventListener(evt,fn);              /*function(e){                 if(option.canBubble){                     e.domEvent.stopPropagation();                 }                 fn.call(this,e);             });*/          }      };            var centerAndZoom = BMap.Map.prototype.centerAndZoom;            BMap.Map.prototype.centerAndZoom  = function(){                    var $this = this;          centerAndZoom.apply(this,arguments);          if(!$this.hasRegistMyTouch){              $this.on("onetouch",function(e){                  //console.log(e);                  var event = document.createEvent("MouseEvent");                  event.initEvent("fastclick",true,true);                                                                        event.clientX = e.clientX;                  event.clientY = e.clientY;                  event.point = e.point;                  $this.dispatchEvent(event);                                    var event = document.createEvent("MouseEvent");                  event.initEvent("click",true,true);                  event.clientX = e.clientX;                  event.clientY = e.clientY;                  $this.dispatchEvent(event);              });              $this.on("longtouch",function(e){                  //console.log(e);                  var event = document.createEvent("TouchEvent");                  event.initEvent("longclick",true,true);                  event.clientX = e.clientX;                  event.clientY = e.clientY;                  event.point = e.point;                  $this.dispatchEvent(event);              });              $this.hasRegistMyTouch = true;          }                }      var MesureEvents = {          onetouch:function($this,evt,fn,data,option){              var ismoved;              var time = null;              var touchLocation = null;              var maxTouchesCount = 0;                            var container = $this.getContainer();              var mask = container.querySelector("div.BMap_mask");              var panes = $this.getPanes();                            $this.addEventListener("touchstart",function(e){                                    var temp = Math.max(maxTouchesCount,e.touches.length);                  if(temp==1) {                      var touch = e.changedTouches[0];                                            if(isAncestors(touch.target,[panes.floatPane,panes.markerMouseTarget,panes.markerPane],container)) return;                                            maxTouchesCount = temp;                                            touchLocation = {                          x:touch.clientX,                          y:touch.clientY                      };                      time = new Date().getTime();                  }              });              $this.addEventListener("touchmove",function(e){                  maxTouchesCount = Math.max(maxTouchesCount,e.touches.length);                                    if(maxTouchesCount==1) {                      var touch = e.changedTouches[0];                                            if(isAncestors(touch.target,[panes.floatPane,panes.markerMouseTarget,panes.markerPane],container)) return;                                                                  if(Math.abs(touchLocation.x-touch.clientX)>0 && Math.abs(touchLocation.y-touch.clientY)>0){//解决部分手机对touchmove过分“敏感”的问题                          ismoved = true;                          //console.log("touchmove---");                      }                      else{                          ismoved = false;                      }                  }              });              $this.addEventListener("touchend",function(e){                                    var touches =e.touches.length;                                    if(touches==0){                                            var touch = e.changedTouches[0];                                            if(isAncestors(touch.target,[panes.floatPane,panes.markerMouseTarget,panes.markerPane],container)) return;                                                                  var temp = maxTouchesCount;                      var tempM = ismoved;                      ismoved= false;                      maxTouchesCount = 0;                      if(temp==1 && !tempM /*&& /BMap_mask/.test(e.srcElement.className)*/ && new Date().getTime()-time<500){                                                    var event = document.createEvent("Event");                          event.initEvent("onetouch",true,true);                          var touch = e.changedTouches[0];                                                    event.clientX = touch.clientX;                          event.clientY = touch.clientY;                          event.point =calLngLat($this,event.clientX,event.clientY);                          mask.dispatchEvent(event,fn);                      }                     }              });          },          longtouch:function($this,evt,fn,data,option){              var ismoved;              var time = null;              var timeout;              var maxTouchesCount = 0;              var touchLocation = null;              var container = $this.getContainer();              var mask = container.querySelector("div.BMap_mask");              var panes = $this.getPanes();              $this.addEventListener("touchstart",function(e){                                    var temp = Math.max(maxTouchesCount,e.touches.length);                  if(temp==1) {                      var touch = e.changedTouches[0];                                            if(isAncestors(touch.target,[panes.floatPane,panes.markerMouseTarget,panes.markerPane],container)) return;                                            maxTouchesCount = temp;                                            touchLocation = {                          x:touch.clientX,                          y:touch.clientY                      };                      time = new Date().getTime();                      timeout = setTimeout(function(){                                                    clearTimeout(timeout);                          timeout = null;                          longtouch(e);                      },750);                  }              });              $this.addEventListener("touchmove",function(e){                  maxTouchesCount = Math.max(maxTouchesCount,e.touches.length);                  if(maxTouchesCount==1) {                      var touch = e.changedTouches[0];                                            if(isAncestors(touch.target,[panes.floatPane,panes.markerMouseTarget,panes.markerPane],container)) return;                                            //console.log("move:" +touch.clientX +"," + touch.clientY);                      if(Math.abs(touchLocation.x-touch.clientX)>=2 && Math.abs(touchLocation.y-touch.clientY)>2){//解决部分手机对touchmove过分“敏感”的问题                          ismoved = true;                          //console.log("touchmove---");                          if(timeout){                              clearTimeout(timeout);                              timeout = null;                          }                      }                      else{                          ismoved = false;                      }                  }                  else{                      if(timeout){                          clearTimeout(timeout);                          timeout = null;                      }                  }              });                            function longtouch(e){                  var temp = maxTouchesCount;                  var tempM = ismoved;                  ismoved= false;                  maxTouchesCount = 0;                  if(temp==1 && !tempM){                                        var event = document.createEvent("Event");                      event.initEvent("longtouch",true,true);                      var touch = e.changedTouches[0];                                            event.clientX = touch.clientX;                      event.clientY = touch.clientY;                      event.point =calLngLat($this,event.clientX,event.clientY);                      $this.getContainer().querySelector("div.BMap_mask").dispatchEvent(event);                                        }                 }                            $this.addEventListener("touchend",function(e){                                    var touches =e.touches.length;                                    if(touches==0){                                            var touch = e.changedTouches[0];                                            if(isAncestors(touch.target,[panes.floatPane,panes.markerMouseTarget,panes.markerPane],container)) return;                                            maxTouchesCount = 0;                      ismoved= false;                  }                  if(new Date().getTime()-time<1000){                      if(timeout){                          clearTimeout(timeout);                          timeout = null;                      }                  }              });          }      }            function calLngLat($this,x,y){          var container = $this.getContainer();          var rect = container.getBoundingClientRect();          var y = y - rect.top;          var x = x - rect.left;          var bounds = $this.getBounds();          var lefTop = new BMap.Point(bounds.getSouthWest().lng,bounds.getNorthEast().lat);          var lefTopPix = $this.pointToPixel(lefTop);          var pix = new BMap.Pixel(lefTopPix.x + x,lefTopPix.y+y);          var point = $this.pixelToPoint(pix);          return point;      }            function inArray(obj,array){          for(x in array){              if(obj==array[x]) return true;          }          return false;      }            function extend(o1,o2){          if(o1 && o2){              for(x in o2){                  if(o2.hasOwnProperty(x) && o2[x]!=undefined){                      o1[x] = o2[x];                  }              }          }      }            function isAncestors(element,nodes,root){          var p = element;          while(p && p!=root){              if(inArray(p,nodes)){                  return true;              }              p = p.parentElement;          }          return false;      }        })(BMap);

 

转载地址:http://corxf.baihongyu.com/

你可能感兴趣的文章
openssl make install : *** [install_docs] 错误 255”
查看>>
chrome 扩展开发需要了解的
查看>>
java调用支付宝支付接口
查看>>
nginx + PHP 下 添加 https 的支持
查看>>
iptables: Applying firewall rules: iptables-restore: line 17 failed
查看>>
只允许指定用户登录SSH
查看>>
MongoDB 3.0 用户创建
查看>>
amazon s3cmd 安装、批量下载
查看>>
elasticsearch 添加settings and mapping
查看>>
Flask 扩展 国际化 本地化
查看>>
阿德勒哲学课
查看>>
java基础学习心得
查看>>
Java文件操作pathSeparator、 pathSeparatorChar、 separator、 separatorChar四者的区别及字节流与字符流
查看>>
Tomcat8配置
查看>>
stl算法:next_permutation剖析
查看>>
斐波那契数列通项公式的几种方法
查看>>
windows下python3.6 通过pip安装Twisted模块报utf-8错误的解决办法
查看>>
Hibernate入门经典
查看>>
操作系统面试
查看>>
接口测试简介
查看>>