Monday 26 June 2017

OpenLayers Select a feature from attribute or id


Using Openlayers.....Is it possible to select a feature on a map by an attribute? eg: Select a feature from a dropdown menu populated with the feature "id" or a table field attribute values.



Answer



You can either use the getFeaturesByAttribute method, or loop through the layer's features array:


for(var f=0;f  if(layer.features[f].attributes.someAttribute == 'desiredValue') {
selectFeatureControl.select(layer.features[f]);
break;
}

}

This assumes you have an active instance of OpenLayers.Control.SelectFeature named "selectFeatureControl", a vector layer named "layer" and you are looking for feature having someAttribute loosely equal to 'desiredValue'. If you have to consider not only the value but also data type (as getFeaturesById does), replace == with ===.


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...