contextmenu event on map not working ?

500 views
Skip to first unread message

Onox

unread,
Nov 4, 2014, 8:16:40 AM11/4/14
to ol3...@googlegroups.com
Hi,

Is it possible to listen to the 'contextmenu' event on a map object ? For me the following does nothing at all (with 'dblclick' instead, it works) :

map.on('contextmenu', function(evt) {
 
var feature = map.forEachFeatureAtPixel(evt.pixel,
   
function(feature, layer) {
     
return feature;
   
});
   
if (feature) {
      evt
.preventDefault();
     
// ...
   
}
});

I am using version 3.0.0.
Thanks

Antonio Santiago

unread,
Nov 4, 2014, 9:41:08 AM11/4/14
to OL3 Dev on behalf of Onox
See docs, there is no 'contextmenu' event for ol.Map: http://openlayers.org/en/master/apidoc/ol.Map.html

You can listen for click event and check if it was the right button.

--
You received this message because you are subscribed to the Google Groups "OL3 Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ol3-dev+u...@googlegroups.com.
To post to this group, send email to ol3...@googlegroups.com.
Visit this group at http://groups.google.com/group/ol3-dev.
To view this discussion on the web visit https://groups.google.com/d/msgid/ol3-dev/089dae24-5ae3-4a35-893b-7f5fb60ba5cf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
|----------------------------------------------------------------
| The book of OpenLayers 3 - http://kcy.me/x29y
| OpenLayers Cookbook: http://kcy.me/ar2b
| Blog: http://www.acuriousanimal.com/blog
| Twitter: @acanimal
|----------------------------------------------------------------
|   _      __                           
|  /_)    (_ ` _   _  _)_ o  _   _   _  
| / /  o .__) (_( ) ) (_  ( (_( (_( (_) 
|                                 _)    
|----------------------------------------------------------------

Onox

unread,
Nov 4, 2014, 11:20:18 AM11/4/14
to ol3...@googlegroups.com
Thanks for your answer, but I can't get it to work. I tried testing for the originalEvent.button subproperty of the 'click' and 'singleclick' events, but actually when I right-click no event is fired at all. How do you do it please ?

Eric Lemoine

unread,
Nov 4, 2014, 12:25:56 PM11/4/14
to OL3 Dev on behalf of Tim Schaub


Le 4 nov. 2014 17:20, "Onox via OL3 Dev" <ol3-dev+noreply-APn2wQdKbSciCKHv...@googlegroups.com> a écrit :
>
> Thanks for your answer, but I can't get it to work. I tried testing for the originalEvent.button subproperty of the 'click' and 'singleclick' events, but actually when I right-click no event is fired at all. How do you do it please ?

You use the browser event API or a lib like jQuery to register a "contextmenu" listener on the map viewport. E.g. with jQuery:

$(map.getViewport()).on('contextmenu', function(e) {
  var coord = map.getEventCoordinate(e);
  ...
});

Untested.

Onox

unread,
Nov 5, 2014, 3:26:03 AM11/5/14
to ol3...@googlegroups.com
Thank you

map.getViewport().addEventListener('contextmenu', function (e) { // or $(map.getViewport()).on('contextmenu', function(e) with jQuery
    e
.preventDefault();
   
var feature = map.forEachFeatureAtPixel(map.getEventPixel(e),
       
function (feature, layer) {

           
return feature;
       
});
   
if (feature) {

       
// ...
   
}
});

Jonatas Walker

unread,
Jan 22, 2016, 2:28:45 PM1/22/16
to OL3 Dev
I wrote a plugin to this need. Take a look if you want.

Best regards.

Rics

unread,
Nov 24, 2016, 12:33:20 AM11/24/16
to OL3 Dev
Hi Jonatas,

Can you please let me know, after i install ol3-contextmenu, what do i import to access 'ContextMenu' in angular2?
import { ContextMenu } from ??

Actually i am getting compilation error on 'ContextMenu'..

Thanks

Jonatas Walker

unread,
Nov 24, 2016, 4:48:54 AM11/24/16
to OL3 Dev on behalf of Rics
Hey Rics, could you please open an issue on https://github.com/jonataswalker/ol3-contextmenu/issues?

It is the right place for this conversation.

Have a good day,

Jonatas Walker

--
You received this message because you are subscribed to a topic in the Google Groups "OL3 Dev" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ol3-dev/yCgIcbURw9M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ol3-dev+unsubscribe@googlegroups.com.

To post to this group, send email to ol3...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages