Openlayers:从GeoJson以编程方式选择一个功能

时间:2015-01-26 21:49:11

标签: openlayers geojson

我正在开发一个使用openlayers(版本2.14)来显示Bing层(GeoJSON格式)的项目,我在阅读GeoJSON和显示功能方面没有问题,但我想以编程方式选择一个功能,例如,有一个表格显示所有功能属性(GeoJSON format.sample:

  

{“type”:“FeatureCollection”,                   “features”:[{“type”:“Feature”,“properties”:{},“geometry”:{“type”:“Point”,“coordinates”:[ - 7923751.4232522,5233536.7371399]},“crs”: { “类型”: “姓名”, “属性”:{ “名称”: “瓮:OGC:DEF:CRS:OGC:1.3:CRS84”}}}       ],}

),当我单击表格中的一行时,我想使用该行中的GEOJSON数据选择或突出显示地图上的特定要素。

我该怎么做?

由于

1 个答案:

答案 0 :(得分:1)

您可以使用方法“getFeaturesByAttribute”

或遍历所有功能:

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

Ps:首先需要创建一个select控件并分配你在for循环中使用的变量名(这里是selectFeatureControl)

请在此处查看我的示例:http://jsfiddle.net/expedio/sh9wv4m7/http://jsfiddle.net/3p5q0ybh/