HTML5:百度地图手机端单触点单击和长按事件,解决部分手机,有需要的朋友可以参考下。
下面是JS文件
[javascript]view plaincopy
- /**
- *AuthorYX
- *
- *对百度地图的事件扩展,目前扩展了fastclick和longclick,
- *解决某些设备click不执行的问题
- *解决长按事件在拖动、多触点依然执行的bug
- *v1.0.0
- */
- (function(){
- BMap.Map.prototype.on=function(evt,fn,data,option){
- if(!evt||!fn)return;
- var$this=this;
- varevtList=["longtouch","onetouch"];
- if(inArray(evt,evtList)){
- MesureEvents[evt]($this,evt,fn,data,option);
- $this.getContainer().querySelector("div.BMap_mask").addEventListener(evt,fn);
- }
- else{
- $this.addEventListener(evt,fn);
- }
- };
- varcenterAndZoom=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);
- varevent=document.createEvent("MouseEvent");
- event.initEvent("fastclick",true,true);
- event.clientX=e.clientX;
- event.clientY=e.clientY;
- event.point=e.point;
- $this.dispatchEvent(event);
- varevent=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);
- varevent=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;
- }
- }
- varMesureEvents={
- onetouch:function($this,evt,fn,data,option){
- varismoved;
- vartime=null;
- vartouchLocation=null;
- varmaxTouchesCount=0;
- $this.addEventListener("touchstart",function(e){
- maxTouchesCount=Math.max(maxTouchesCount,e.touches.length);
- if(maxTouchesCount==1){
- vartouch=e.changedTouches[0];
- touchLocation={
- x:touch.clientX,
- y:touch.clientY
- };
- time=newDate().getTime();
- }
- });
- $this.addEventListener("touchmove",function(e){
- maxTouchesCount=Math.max(maxTouchesCount,e.touches.length);
- if(maxTouchesCount==1){
- vartouch=e.changedTouches[0];
- 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){
- vartouches=e.touches.length;
- if(touches==0){
- vartemp=maxTouchesCount;
- vartempM=ismoved;
- ismoved=false;
- maxTouchesCount=0;
- if(temp==1&&!tempM/*&&/BMap_mask/.test(e.srcElement.className)*/&&newDate().getTime()-time<500){
- varevent=document.createEvent("Event");
- event.initEvent("onetouch",true,true);
- vartouch=e.changedTouches[0];
- event.clientX=touch.clientX;
- event.clientY=touch.clientY;
- event.point=calLngLat($this,event.clientX,event.clientY);
- varmask=$this.getContainer().querySelector("div.BMap_mask");
- mask.dispatchEvent(event,fn);
- }
- }
- });
- },
- longtouch:function($this,evt,fn,data,option){
- varismoved;
- vartime=null;
- vartimeout;
- varmaxTouchesCount=0;
- vartouchLocation=null;
- $this.addEventListener("touchstart",function(e){
- maxTouchesCount=Math.max(maxTouchesCount,e.touches.length);
- if(maxTouchesCount==1){
- vartouch=e.changedTouches[0];
- //console.log("one:"+touch.clientX+","+touch.clientY);
- touchLocation={
- x:touch.clientX,
- y:touch.clientY
- };
- time=newDate().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){
- vartouch=e.changedTouches[0];
- //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;
- }
- }
- });
- functionlongtouch(e){
- vartemp=maxTouchesCount;
- vartempM=ismoved;
- ismoved=false;
- maxTouchesCount=0;
- if(temp==1&&!tempM){
- varevent=document.createEvent("Event");
- event.initEvent("longtouch",true,true);
- vartouch=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){
- vartouches=e.touches.length;
- if(touches==0){
- maxTouchesCount=0;
- ismoved=false;
- }
- if(newDate().getTime()-time<1000){
- if(timeout){
- clearTimeout(timeout);
- timeout=null;
- }
- }
- });
- }
- }
- functioncalLngLat($this,x,y){
- varcontainer=$this.getContainer();
- varrect=container.getBoundingClientRect();
- vary=y-rect.top;
- varx=x-rect.left;
- varbounds=$this.getBounds();
- varlefTop=newBMap.Point(bounds.getSouthWest().lng,bounds.getNorthEast().lat);
- varlefTopPix=$this.pointToPixel(lefTop);
- varpix=newBMap.Pixel(lefTopPix.x+x,lefTopPix.y+y);
- varpoint=$this.pixelToPoint(pix);
- returnpoint;
- }
- functioninArray(obj,array){
- for(xinarray){
- if(obj==array[x])returntrue;
- }
- returnfalse;
- }
- })(BMap);
转载博客:http://blog.csdn.net/yyyuuueeee/article/details/49511173
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。