我知道谷歌地图中没有“长按”事件。但是我以某种方式使用其他事件的组合来模拟这种行为。按下一段时间后,会出现一个带有选项的上下文菜单。但是,在平板电脑等支持捏合缩放的设备中,当我用两根手指放大或缩小后,当我停止触摸设备时,菜单意外出现。两个并发的触摸事件很可能会干扰另一个。任何有建议的人表示赞赏。以下是相关代码:
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(); });