-1

I have a map with some Overlays (DIVs with a text content) on it. I have "click" event registered for the map like:

map.on('click', function(evt) { 
    var click_coordinates = evt.coordinate;
});

It works fine until a user click an Overlay. In that case the event is probably catch by the Overlay, but it's not propagated to the map. It's OK for some situations, but sometimes I need the event to be handled by the map. So I catch the click event on the Overlay and I try do send it to the map element by

map.dispatchEvent("click");

The good thing is, the event on the map is fired, unfortunately it's "different" object, than originaly as it contains no "coordinate" attribute and other Openlayers stuff. Is there a way, how to dispatch the event in the same way as natural click on the map?

Jonatas Walker
  • 13,583
  • 5
  • 53
  • 82
user3523426
  • 836
  • 3
  • 11
  • 26

1 Answers1

1

You can achieve with this:

// content is ol.Overlay#element therefore a DOM element
content.addEventListener('click', function(evt){
  map.dispatchEvent(evt);
});

map.on('click', function(evt){
  var pixel = [evt.clientX, evt.clientY];
  var coord = map.getCoordinateFromPixel(pixel);

  console.info('event', evt);
  console.info('pixel', pixel);
  console.info('coord', coord);
});

Or simply:

content.addEventListener('click', function(evt){
  var pixel = [evt.clientX, evt.clientY];
  var coord = map.getCoordinateFromPixel(pixel);

  console.info('event', evt);
  console.info('pixel', pixel);
  console.info('coord', coord);
});
Jonatas Walker
  • 13,583
  • 5
  • 53
  • 82
  • Probably, I am missing the point, but this will give me standard event object instead of OL even object ( e.g. evt.coordinate is missing). It means the same procedure cannot be used for handling map click and object click, or an object click event has to be pre-transformed (e.g. var coord = map.getCoordinateFromPixel(pixel) for getting the coordinates) before passing to map.click procedure. That's why I was asking about "how to dispatch the event in the same way as natural click on the map" as I do believe OL has a nature way of doing it. – user3523426 Jun 05 '16 at 14:30
  • Then your answer is **NO, there's no way to do it**. – Jonatas Walker Jun 05 '16 at 22:37
  • 1
    In my answer I said "You can **achieve** this with ..." – Jonatas Walker Jun 05 '16 at 22:38