Search code examples
event-handlingopenlayers

OpenLayers 2 trigger event when baselayer changes


I want my application to do something when the base layer has changed.

Is it possible to handle/catch that event in OpenLayers 2?


Solution

  • Yes, it is possible:

    map.events.register("changebaselayer", this, function (obj) {
        if (obj.layer.name == 'layer_name') {
           //do something if new base layer is equal to layer_name
           ....
       }
    });
    

    You can see all the possible events you can hook into in the source for OpenLayers/Map.js although the actual event will be triggered by the LayerSwitcher.