[GIS] Selecting feature programmatically from GeoJSON using OpenLayers

geojsonopenlayers-2

I am working on a project that uses OpenLayers (version 2.14)to display a Bing layer(GeoJSON format), I have no problem reading the GeoJSON and display features, but I want to select a feature programmatically, for example, there is a table displaying all the features attributes(GeoJSON format.sample:

{"type": "FeatureCollection", "features": [
{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[-7923751.4232522,5233536.7371399]},"crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:OGC:1.3:CRS84"}}}
], }

), when I click a row from the table, I want to select or highlight a specific feature on the map using the GEOJSON data in that row.

How can I do that?

Best Answer

You could use the method "getFeaturesByAttribute"

or iterate through all features:

 for(var i = 0; i < yourgeojsonlayer.features.length; i++) { 
    if(yourgeojsonlayer.features[i].attributes.searchedAttribute == 'searchedValue')
     { selectFeatureControl.select(yourgeojsonlayer.features[i]); break; } 
    }

Ps: makes necessary to create a select control first and assign the variable name you use in the for loop (here selectFeatureControl)

I just wrote down a quick example here: http://jsfiddle.net/expedio/sh9wv4m7/