百度地图手机端单触点单击和长按事件,解决部分手机(小米手机)地图单击事件失效,多触点、拖动依然触发长按的bug
/**
* 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);
百度地图手机端单触点单击和长按事件,解决部分手机(小米手机)地图单击事件失效,多触点、拖动依然触发长按的bug的更多相关文章
- fullpage 单屏高度超过屏幕高度,实现单屏内可以滚动并解决手机端单屏高度不正确的问题
最近接触了好几次jquery.fullpage.js这个插件,实现整屏的滑动,效果很炫,用fullpage来实现也很简单,但是也碰到了一些问题和大家分享一下 1.单屏高度超过屏幕高度,实现单屏的滑动 ...
- [js开源组件开发]-手机端照片预览组件
手机端照片预览组件 可怜的我用着华为3C手机,用别人现成的组件都好卡,为了适应我这种屌丝,于是自己简化写了一版的照片预览效果,暂时无缩放功能,以后可能有空再加吧,你也可以自己加下,这是个github上 ...
- web端和手机端测试有什么不同
面试中经常被问到web端测试和手机端测试有什么相同点和区别呢?现在总结一下这个问题,如有不对敬请指正 web端和手机端测试有什么区别 1.相同点 不管是web测试还是手机App测试,都离不开测试的相关 ...
- 移动端调试神器vconsole,手机端网页的调试工具Eruda
移动端调试神器vconsole,手机端网页的调试工具Eruda 移动端中使用 vConsole调试 移动端调试工具vconsole安装Git地址:https://github.com/WechatFE ...
- 织梦M手机端/自适应网站内容图片变形解决办法
我们在做响应式网站或者织梦M功能手机站的时候,会发现如果内容页图片太大,在移动端显示会变形,dede手机端图片过长等问题,手机端文章内容页图片不能自适应!这给通过手机端浏览网站的用户造成了很不好的用户 ...
- 【百度地图JavaScript API】手机端浏览器定位的实现
[百度地图JavaScript API]手机端浏览器定位的实现 https://blog.csdn.net/xiao190128/article/details/72579476
- js replace 全局替换 以表单的方式提交参数 判断是否为ie浏览器 将jquery.qqFace.js表情转换成微信的字符码 手机端省市区联动 新字体引用本地运行可以获得,放到服务器上报404 C#提取html中的汉字 MVC几种找不到资源的解决方式 使用Windows服务定时去执行一个方法的三种方式
js replace 全局替换 js 的replace 默认替换只替换第一个匹配的字符,如果字符串有超过两个以上的对应字符就无法进行替换,这时候就要进行一点操作,进行全部替换. <scrip ...
- 手机端的表单验证和PC端的不同
1.手机端:由于页面小的局限性,表单验证从上到下依次进行,如果上一个验证不通过,则给出错误提示,代码中return回去,不必进行下一个的校验: 2.PC端:页面范围大,一般是在表单的后面或者下面,提示 ...
- 表单界面的兼容PC手机端解决方案
就当写一篇随笔吧~上星期还在做加盟模块(兼容微信端),这星期已经加班做快递扫码模块(react+node),所以我感觉只有弹药备足了才能稍微轻松些应对各种需求.实话说在同个部门的大佬面前差距确实大,如 ...
随机推荐
- 利用卷积神经网络(VGG19)实现火灾分类(附tensorflow代码及训练集)
源码地址 https://github.com/stephen-v/tensorflow_vgg_classify 1. VGG介绍 1.1. VGG模型结构 1.2. VGG19架构 2. 用Ten ...
- iOS APP 中H5视频默认全屏播放问题解决
问题描述:在Android中,视频可以正常在H5页面局部播放,iOS中则自动切换至全屏模式. 查看资料得以解决,20190301记录下来. 解决方法:IOS10及以后,在 video标签页中只包含 w ...
- linux内实践核分析模块
- MySQL主从复制配置遇到的部分问题
网上配置教程很多,我也是参考其他人的教程完成的,主要遇到了以下几个问题,如果以后有人遇到相同的希望能够给大家写提示吧. 1.my.cnf文件配置 Master上的my.cnf中配置的server_id ...
- cglib 动态代理
JDK的动态代理比较慢,可以使用cglib的代理,速度比较快: package cn.demo02; import java.lang.reflect.Method; import java.util ...
- 第三个Sprint ------第四天
出题代码 package com.app.senior_calculator; import java.io.Serializable; public class Question implement ...
- 第三个spring冲刺第5、6、7天(三天汇总)
这三天我们一直在研究选择题的选项插入与切换,现在已经大致完善了,达到了预想的效果,晚点会补上截图.
- BugPhobia进阶篇章:前端技术/设计文档
0x01 :前端概述 0x0100 :前端基本描述 前端基础框架 Semantic UI 根据http://semantic-ui.com/提供的样例和文档,依据Version 2.1.4版本的特性进 ...
- shell脚本--数值计算
原生bash不支持简单的数学运算,即使是最简单的加减乘除 但是,可以使用$[]和expr来实现整数运算 如果要实现小数运算,可以使用bc命令 使用$[]来实现: #!/bin/bash #文件名:te ...
- linux 终端快捷操作
终端一些常用快捷键 按键 作用 Ctrl+d 键盘输入结束或退出终端 Ctrl+s 暂停当前程序,暂停后按下任意键恢复运行 Ctrl+z 将当前程序放到后台运行,jobs命令查看后台工作,命令&quo ...