0

I added a panel to a map that is contained inside an IFrame. Then I attach a click handler to the div of this panel with JQuery to toggle a control.

However, I notice that a click on the panel also trigger a map click event when it should not because another control handle the map click and both the panel and the map controls are triggered together.

var olmap = iframe.contentWindow.map // openlayers map inside the iframe
var i = olmap.getControlsByClass("Korem.Control.Info")[0] //map click control
var layerControl = olmap.getControlsByClass("Korem.Control.LightLayerControl")[0]
var customPanel = new iframe.contentWindow.OpenLayers.Control.Panel({
  id: "customPanel",
  autoActivate: true
});
olmap.addControl(customPanel)
iframeBody = $(iframe).contents().find("body");
var panel= iframeBody.find("#customPanel")
panel.css({"position":"absolute", "top":"0", "right":"0", "width":"auto", "height":"auto", "z-index":"1033"})
panel.append('<div id="clickLink" class="MenuItem1ItemInactive" style="cursor:pointer; padding:2px 5px; border:1px solid white; font-size: 12px; font-family: Arial;">Afficher les écoles</div>');
$(iframe).contents().find("#clickLink").click(function(){
    i.deactivate()
    if (layerControl.active == true) {
      layerControl.deactivate();
    } else {
      layerControl.activate();
    }
});
layerControl.events.on({deactivate : function(){
    i.activate();
  }
});

Is it normal that a click on a panel trigger a map click also? How could I prevent it?

1
  • Normally, no, click events are restricted to the map div itself. However, I think your issue is because your map is part of the iframe, which you then register a click event on. Commented Oct 20, 2014 at 13:58

1 Answer 1

0

It is normal, that OpenLayers registers click events to map instances when constructed. You can try to block them with the stopPropagation() method or prevent them with the preventDefault() method. If you use OpenLayers 2, you can try to unregister them manually, but you have to know which events you want to unregister (https://stackoverflow.com/questions/7637560/cant-unregister-click-event-in-openlayers). OpenLayers 3 API tells to use the above mentioned functions instead.

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.