Kun
Kun

Reputation: 21

How to unselect a feature

I am doing a maplayer switch function but when I switch the layer, the selected feature is not unselected. May I know how to unselect a feature in openlayer 3.

$("#show-field-map").click(function(event) {
    map.removeLayer(subbasinJsonp);
    map.addLayer(fieldJsonp);
    $('#show-subbasin-map').attr("disabled", false);
    $('#show-field-map').attr("disabled", true);
});

$("#show-subbasin-map").click(function(event) {
    map.removeLayer(fieldJsonp);
    map.addLayer(subbasinJsonp);
    $('#show-field-map').attr("disabled", false);
    $('#show-subbasin-map').attr("disabled", true);
});

Upvotes: 1

Views: 4809

Answers (3)

ILiyan Dimitrov
ILiyan Dimitrov

Reputation: 61

For OpenLayers v7:

First declare the select

let select = new Select({
    layers: [someVectorLayer],
});

Then remove it:

map.removeInteraction(select);

Upvotes: 0

Frimlik
Frimlik

Reputation: 439

Well, I would rather choose a way of removing interaction and assigning it again, like it is here.

Upvotes: -2

ahocevar
ahocevar

Reputation: 5647

The Select interaction carries collection of features:

var select = new ol.interation.Select();
var features = selectInteraction.getFeatures();

You can call clear() on that selection to unselect all features:

features.clear();

Upvotes: 6

Related Questions