[GIS] Getting JSON feature id on OpenLayers2

geojsonjavascriptjsonopenlayers-2

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.

Best Answer

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.