0

I want to access the coordinates:

{
    "name":"String",
    "type":"FeatureCollection",
    "features":[
        {"type":"Feature", "geometry": {"type":"Point", "coordinates":[10,11]}},
        {...},
        {...}]
}

I already tried:

var jsonfile = $.getJSON("myjsonfile.json");
for(var i = 0, l = jsonfile.features; i < l; i++) {
  var obj = json.features[i];
  console.log(obj.coordinates[1]);
}

But this doesn't work. I don't know why...

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
plasmid0h
  • 186
  • 2
  • 13

1 Answers1

1
obj.features[0].geometry.coordinates;

The object contains an array called features the first element of which is another object called geometry, one property of which is an array called coordinates.

So, the iteration would be something like:

var jsonfile = $.getJSON("myjsonfile.json", function (data) {
    for (var i = 0, l = data.features.length; i < l; i++) {
        var coords = data.features[i].geometry.coordinates;
        var lat = coords[0];
        var lng = coords[1];
        // plot lat, lng
    }
});
Andy
  • 61,948
  • 13
  • 68
  • 95