Search code examples
eventsopenlayers

Openlayers and events on multiple layers (OpenLayer.Layer.Vector)


Another day working with openlayers and another problem.

Namely - i have multiple vector layers on top of each other for different types of stuff (cars, trips from history and areas). They all have events that im trying to catch... But as Niklas found out, when you activate events on one layer, it gets moved on top and events on layers below wont fire.

Is there a way to bypass this? Because when i move over area polygon i want event firing and displaying its name and when i move mouse over to car marker i want the event fire too. And no - i dont want to put them on same layer, cause i want it to be possible to turn them off or on fast and without looping through all the features and disabling them each.

Alan

Edit1: I did some searching, and found out, that you could use same control on multiple layers. that could probably fix this problem for me. im checking it out atm and testing if adding more layers to single control is solution to my problem or not.


Solution

  • I found this when I had the same issue, trying to get multiple layers to react to mouse events.

    The solution, just in case anyone else finds this thread is much simpler.

    The SelectFeature control takes an array of Vector layers and if all the laters you need to react to mouse events (hover and click) are in that array, they ALL work, not just the one that was moved to the top.

    So, in the approved solution to this thread, it can be much simplified by doing this:

    this.carSelect = new OpenLayers.Control.SelectFeature(
        [this.vectorsLayer, this.carsLayer],
        {
            'hover':true,
            'callbacks': {
                blah blah blah
        }
    });
    

    This will register the appropriate events on both layers and make them both live.

    I hope this helps anyone else stumbling on this issue.

    As said elsewhere, using OpenLayers is not hard, finding the correct way to do things with it is.