我猜Google地图没有长按/按住事件处理程序。下面的代码是实现这个功能的一种方法吗?
我知道这是一个旧帖子,但我遇到了同样的问题,我找到了比@Anbarasan Thangapalam建议的更好的解决方案。
解决方案:
var mousedUp = false;
google.maps.event.addListener(marker, 'mousedown', function(event){
mousedUp = false;
setTimeout(function(){
if(mousedUp === false){
//do something if the mouse was still down
//after 500ms
}
}, 500);
});
google.maps.event.addListener(marker, 'mouseup', function(event){
mousedUp = true;
});
我认为这种方法可能更易于理解,代码行数更少,监听函数也更少。
为了避免在拖动事件上触发超时函数,只需复制 mouseup
函数并将其更改为 dragstart
。像这样:
google.maps.event.addListener(marker, 'dragstart', function(event){
mousedUp = true;
});
我使用鼠标按下和鼠标释放事件监听器实现了长按功能。谢谢!!
var longpress = false;
google.maps.event.addListener(marker,'click', function (event) {
(longpress) ? console.log("Long Press") : console.log("Short Press");
});
google.maps.event.addListener(marker, 'mousedown', function(event){
start = new Date().getTime();
});
google.maps.event.addListener(marker, 'mouseup', function(event){
end = new Date().getTime();
longpress = (end - start < 500) ? false : true;
});
var longPressTimeout = null;
var longPressActive = false;
var longPressed = false;
google.maps.event.addListener(marker, 'mousedown', function(event){
longPressActive = true;
longPressed = false;
longPressTimeout = setTimeout(function(){
if(longPressActive === true){
console.log("Long Press")
longPressed = true;
//do something after marker long pressed for 1000ms
}
}, 1000);
});
google.maps.event.addListener(marker, 'mouseup', function(event){
clearTimeout(longPressTimeout);
longPressActive = false;
});
google.maps.event.addListener(marker, 'dragstart', function(event){
clearTimeout(longPressTimeout);
longPressActive = false;
});
我发表这篇文章作为解决方案,因为我还没有足够的声望来发表评论。
对我来说,以下方法有效。要查看它是右键单击
还是长按
,只需在处理程序中检查事件类型是否为"contextmenu"。
gMaps.event.addListener(overlay, 'click', function() {
if(event.type === "contextmenu"){
//longpress handler codes
}
}