9

I would like my KML layer in OpenLayers 3 (OL3) to respond to different types of click events.

I have a single click method working nicely. But how do I get it working for "shift-clicks" "double clicks" etc?

 map.on('singleclick', function(evt) {                         
   var feature = map.forEachFeatureAtPixel(evt.pixel,
     function(feature, layer) {
     // do stuff here with feature
     return [feature, layer];                                  
   });                                                         
 });                                                           

This was a good start, but I can't quite put the OL3 API documentation together.

0

2 Answers 2

10

ah ok dblclick is the answer

// set mode to current mode of double clicked feature                       
map.on('dblclick', function(evt) {
  console.log('doubled');
  var feature = map.forEachFeatureAtPixel(evt.pixel,
    function(feature, layer) {
      // do stuff here
    });
});                                                                    

and evt.originalEvent.altKey, evt.originalEvent.ctrlKey, evt.originalEvent.metaKey, evt.originalEvent.shitKey are all availabe within the handler!

3

Get clicked XY coordinates in ol3

map.on('click', function(evt) {
var coordinates = evt.coordinate;
alert(coordinates);
});
2
  • Can I get latitude and longitude here? Commented Jan 13, 2020 at 7:13
  • What if you want to get coordinates of two same event (such as a mouse 'click') but separated in time in different variable without updating the coordinates of the first event? Commented Jan 22, 2020 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.