Wednesday, 22 April 2015

How can I unselect a feature in Openlayers?


I am able to identify the selected features on my map using the following line:


vector_layer.selectedFeatures


I would like to be able to deselect this feature using a standalone function (not an event-handler). I've tried using the functions unselect() and unselectAll() unsuccessfully and can't find any examples of this approach.


I am aware that clicking on the selected object can be used to unselect. In this case I want a programmatic solution.



Answer



OpenLayers 2: You can unselect features with unselect and unselectAll methods of SelectFeature control:


selectControl = new OpenLayers.Control.SelectFeature(vectorLayer);
...
map.addControls([selectControl]);
selectControl.activate();

// unselect any specific feature...

selectControl.unselect(vectorLayer.features[0]);
// ...or all features
selectControl.unselectAll();

If this doesn't work, there's probably bug in your code.


Here is fiddle: http://jsfiddle.net/dHxnh/1/


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...