daws
daws

Reputation: 33

Interaction.defaults on specific vectorLayer and added after the map is already initialized

I'm in trouble with ol.interaction.defaults().extend(). I hope you can help me :)

I re-implemented the Drag feature example On a plunker for test.

var dragInteraction = new ol.interaction.defaults().extend([new app.Drag()]);

var map = new ol.Map({
  interactions: dragInteraction,
  layers: [
    new ol.layer.Tile({
      source: new ol.source.TileJSON({
        url: 'http://api.tiles.mapbox.com/v3/mapbox.geography-class.jsonp'
      })
    }),
    layerA,
    layerB
  ],
  target: 'map',
  view: new ol.View({
    center: [0, 0],
    zoom: 2
  })
});

//map.addInteraction(dragInteraction);

I have to issues :

  1. I want to enable the dragInteraction only for the layerA (not layerB); how can I do that ?
  2. I want to add this interaction AFTER that the map has been created. But wen I try, there is an execution error. I don't know if its possible to ovveride defaults interaction once the map has been created ?

I searched on OL3-Dev & API doc, but didn't found the correct solution.

notes :

Thks for your help :)

Upvotes: 1

Views: 1425

Answers (2)

Jonatas Walker
Jonatas Walker

Reputation: 14150

Like suggestion above. Give a name for layer, implement a layer filter and add the interaction after the map creation.

I forked your plunker as an example.

Regards

Upvotes: 1

tsauerwein
tsauerwein

Reputation: 6011

I want to enable the dragInteraction only for the layerA (not layerB); how can I do that ?

You can always add a new interaction after the initialization of the map with:

map.addInteraction(new app.Drag());

I want to add this interaction AFTER that the map has been created. But wen I try, there is an execution error. I don't know if its possible to ovveride defaults interaction once the map has been created ?

In the drag interaction you are using map.forEachFeatureAtPixel which takes a parameter layerFilter. You can use it like so:

var layerFilter = function(layer) {
  return layer === layerA;
}

var feature = map.forEachFeatureAtPixel(evt.pixel,
    function(feature, layer) {
      return feature;
    }, this, layerFilter, this);

Upvotes: 1

Related Questions