2011-12-07 80 views
0

查看dev.openlayers.org/apidocs/files/OpenLayers/Layer/Vector-js.htm之後 - 不清楚如何從geojson中提取屬性值下面的實施例,使用以下的javascript:從OpenLayers.Layer.Vector對象中檢索GeoJson屬性

{ "type": "FeatureCollection", 
    "features": [ 
    { "type": "Feature", 
     "geometry": 
      { "type": "MultiPoint", 
       "coordinates": [[[0,0]]] 
      }, 
     "properties": {"test" : "this"} 
     } 
    ] 
} 


layer = new OpenLayers.Layer.Vector("GML", { 
      strategies: [new OpenLayers.Strategy.Fixed()], 
      protocol: new OpenLayers.Protocol.HTTP({ 
      url: "some_url", 
      params: {...}, 
      format: new OpenLayers.Format.GeoJSON() 
      }), 
     }); 

我所知到目前爲止的方式是,層是具有含「屬性」爲特徵類型的屬性的對象。但不知道如何訪問它。

任何幫助,將不勝感激。提前致謝!

回答

2

它是具有來自geoj​​son文件屬性的圖層的特徵,而不是圖層本身。你可以像這樣訪問他們:

for(var i=0; i < layer.features.length; i++){ 
    console.log(layer.features[i].attributes.test); 
} 

所以,要素對象的屬性attributes將所有的屬性。