Openlayers:以编程方式从GeoJson中选择一个要素 [英] Openlayers: select a feature programmatically from GeoJson

查看:172
本文介绍了Openlayers:以编程方式从GeoJson中选择一个要素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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":名称",属性":{名称":"ur:ogc:def:crs:OGC:1.3:CRS84"}}} ],}

{"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"}}} ], }

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

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

我该怎么做?

谢谢

推荐答案

您可以使用方法"getFeaturesByAttribute"

You could use the method "getFeaturesByAttribute"

或遍历所有功能:

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

Ps:必须首先创建一个选择控件并分配您在for循环中使用的变量名(此处为selectFeatureControl)

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

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

See my examples here: http://jsfiddle.net/expedio/sh9wv4m7/ and http://jsfiddle.net/3p5q0ybh/

这篇关于Openlayers:以编程方式从GeoJson中选择一个要素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆