5

I have implemented a vector layer(using JavaScript) in OpenLayers which has a point geometry.

Now my requirement is to show menu just beside the point whenever we do right-click on the point.

Can we do this in OpenLayers?

2
  • It's doable, but not straight forward. Right clicks are not picked up by javascript by default (they are reserved for browser menus). Commented May 8, 2012 at 13:55
  • If you try OL 3 I wrote an extension for this. Commented Jan 22, 2016 at 19:32

1 Answer 1

7

there is an information here about right click select feature. i havent try it but it looks like it works.

myMap is OpenLayers.Map object and myVectorLayer is... OpenLayers.Layer.Vector.

myMap.div.oncontextmenu = function noContextMenu(e) { if(!e){ //dear IE... var e = window.event; e.returnValue = false; } var f = myVectorLayer.getFeatureFromEvent(e); alert(f); //f is the pointed vector.feature :) return false; //Prevent display of browser context menu } 

i hope it helps you...

3
  • "getFeatureFromEvent(e)" was really a good pointer...I have used this to get point geometry(which is laid over vector feature) and further to show menu(for which I have used openlayer's popup)I have used (long,lat) of point. Following is the code which I have implmeneted to show menu besides with point geometry: Commented May 9, 2012 at 10:25
  • 1
    var f = vectorLayerForHoleMarkers.getFeatureFromEvent(e); popup = new OpenLayers.Popup("chicken", new OpenLayers.LonLat(f.geometry.x,f.geometry.y), new OpenLayers.Size(200,200),true); popup.closeOnMove = true; myMap.addPopup(popup); Commented May 9, 2012 at 10:28
  • myMap.div ? TypeError: map.div is undefined ... Commented Jan 11, 2017 at 10:27

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.