1

I am working on a project that uses openlayers (version2.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?

Thanks

0

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/

  • Imagine ,i clicked on table and get Following geojson : var featurejson= {"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"}}} ], }; – Ismayil Jan 27 '15 at 7:54
  • vlayer = new OpenLayers.Layer.Vector("Editable"); map.addLayer(vlayer); geojson_format = new OpenLayers.Format.GeoJSON(); geojson_format.ignoreExtraDims = true; var ft = new OpenLayers.Feature.Vector(geojson_format.read(featuredata),{scientific_name:'hydro'}); vlayer.addFeatures([ft]); var features = vlayer.features; var selectControl = new OpenLayers.Control.SelectFeature(vlayer, {multiple: true,toggle: false}); window.map.addControl(selectControl); selectControl.activate(); – Ismayil Jan 27 '15 at 8:05
  • selectFeature.select(vlayer.getFeatureBy('scientific_name', 'hydro')); – Ismayil Jan 27 '15 at 8:05
  • I do that,but it is not working.Can you help me ,how can I do that with "getFeatureBy" or "getFeaturesByAttribute" – Ismayil Jan 27 '15 at 8:08
  • 1
    just post your code here at stackexchange or jsfilldle so others can also help you or benefit from an answer that's found – Thomas B Jan 27 '15 at 10:15

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.