我知道谷歌地图中没有“长按”事件。但我以某种方式使用其他事件的组合来模拟这种行为。按下一段时间后,会出现一个带有选项的上下文菜单。但是,在支持捏合缩放的设备(例如平板电脑)中,当我使用两根手指放大或缩小后,当我停止触摸设备时,菜单会意外出现。两个并发的触摸事件很可能会干扰另一个。任何有建议的人都会受到赞赏。这是相关代码:
var contextMenuOptions={};
contextMenuOptions.classNames={menu:'context_menu', menuSeparator:'context_menu_separator'};
var menuItems=[];
menuItems.push({className:'context_menu_item', eventName:'start_click', label:'From here'});
menuItems.push({className:'context_menu_item', eventName:'end_click', label:'To here'});
menuItems.push({className:'context_menu_item', eventName:'from_current_loc', label:'From my location'});
menuItems.push({className:'context_menu_item', eventName:'to_current_loc', label:'To my location'});
// menuItems.push({className:'context_menu_item', eventName:'center_map_click', label:'Center map here'});
contextMenuOptions.menuItems=menuItems;
// create the ContextMenu object
var contextMenu=new ContextMenu(map, contextMenuOptions);
google.maps.event.addListener(map, "mousedown", function(event){
contextMenu.hide();
sayac = setTimeout(function(){ contextMenu.show(event.latLng); }, 500);
});
// main refers to map container
document.getElementById("main").addEventListener("touchend",function(){ clearTimeout(sayac); });
document.getElementById("main").addEventListener("touchmove", function(){ clearTimeout(sayac); });
document.getElementById("main").addEventListener("click",function(e){ e.preventDefault(); });
最佳答案
您可以绑定(bind)以下事件来覆盖平板电脑事件,并仅在处理程序内执行返回。
google.maps.event.addListener(map, "touchstart", function(e){
return;
});
google.maps.event.addListener(map, "touchend", function(e){
return;
});
在这里您可以阅读有关touchstart的更多信息。和 touchend事件。
关于javascript - 谷歌地图javascript api长按行为被缩放捏破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15891493/