Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

  • Forcing an OpenLayers Markers layer to draw on top, and having selectable layers beneath
like image 429
Odif Yltsaeb Avatar asked Apr 08 '11 11:04

Odif Yltsaeb


People also ask

What is vector layers in OpenLayers?

import VectorLayer from 'ol/layer/Vector'; Vector data is rendered client-side, as vectors. This layer type provides most accurate rendering even during animations.

How do you remove layers from OpenLayers?

filter(layer => layer. get('name') === 'Marker') . forEach(layer => map. removeLayer(layer));

What is the use of OpenLayers?

OpenLayers makes it easy to put a dynamic map in any web page. It can display map tiles, vector data and markers loaded from any source. OpenLayers has been developed to further the use of geographic information of all kinds.


2 Answers

this solve my problem :

before :

      map.addLayer(layer);
      layer.events.register("loadend", layer, function(){
        $("#progress").hide();
      });

after :

      layer.events.register("loadend", layer, function(){
        $("#progress").hide();
      });
      map.addLayer(layer);

Hope it helps

like image 29
Jakikiller Avatar answered Nov 14 '22 08:11

Jakikiller


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.

like image 56
Martin Fraser Avatar answered Nov 14 '22 09:11

Martin Fraser