3

I'm projecting some features using JSON format on OpenLayers. I need to get the id from the feature (not the OpenLayers' id but the one in the JSON file). The file looks something like this.

{"type": "FeatureCollection","features": [{ "type": "Feature", "id": 7655199, "properties": {"power":"line","wires":"NA","frequency":"NA","voltage":"380000","operator":"NA","cables":"6"}, "geometry": {"type":"LineString", "coordinates":[[8.5310699,49.8531139],[8.5289255,49.8563936],[8.5268405,49.8594383]]}} etc..

Now in OpenLayers I'm trying to do something like this.

var feature_col = gjson.read(ReadFile(json_path + file[i]) , "FeatureCollection");
//Readfile is just a function for reading the file, what a surprise... :P
var power_res = feature_col[0].attributes.power;
var id_res = feature_col[0].id) //for the first feature

So the value in power_res gives line which is correct. However, the value in id_res is something like OpenLayers.Feature.Vector_362 and I need the id in this case to be 7655199.

2

Try to use var id_res = feature_col[0].attributes.id instead var id_res = feature_col[0].id.

UPDATE

feature_col[0].fid See my live example here.

  • It doesn't work. It is undefined. Thanks anyway – zom-pro Jul 16 '12 at 5:34
  • I've modified my answer, check it out. – drnextgis Jul 16 '12 at 5:55
2

Ok, I found it. feature_col[0].fid is the way. It results into 7655199. FID not ID.

source: http://chitraswiki.blogspot.de/2009/11/getting-attributes-of.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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