openlayers2地图右键菜单

时间:2016-08-09 04:08:10

标签: javascript openlayers

我想在右键单击时在地图上添加菜单,我尝试了很多方法,但是不起作用。    一种方式,如跟随,但不工作    我注册了一个onMapRightClick函数来右键单击,但当我右键单击地图时,它没有响应

openlayer event.js中的代码

 BROWSER_EVENTS: [
    "mouseover", "mouseout",
    "mousedown", "mouseup", "mousemove", 
    "click", "dblclick", "rightclick", "dblrightclick",
    "resize", "focus", "blur",
    "touchstart", "touchmove", "touchend",
    "keydown"
],
我的js文件中的

代码

 map.events.register("rightclick", map, onMapRightClick);

 function onMapRightClick(e){
 var str = map.getLonLatFromViewPortPx(e.xy);
 Ext.MessageBox.show("",str.lat + " " + str.lon);
 };

如果有办法可以帮助我

1 个答案:

答案 0 :(得分:0)

您可以让您的事件处理程序聆听' mouseup'事件并使用OpenLayers内置的功能来确定它是否是一个右键单击。请记住禁用地图元素上的上下文菜单。

// Disable the context menu on your map element.
var mapElement = document.querySelector("#map");
mapElement.oncontextmenu = function () { return false };

// Create the map and add your event listener.
var map = new OpenLayers.Map('map');
map.events.register('mouseup', map, onMapRightClick);

function onMapRightClick(e) {

  // Check if it is a right click event.
    if (OpenLayers.Event.isRightClick(e)) {
      var str = map.getLonLatFromViewPortPx(e.xy);
      Ext.MessageBox.show("",str.lat + " " + str.lon);
    }
}