循环遍历 GeoJSON 属性

kao*_*ify 3 javascript geojson

我正在尝试循环访问 GeoJSON 功能中的特定属性,但遇到了一些麻烦。以下是我尝试在本地服务器上实现它的方法:

$.getJSON('myData.geojson', function(data) {
    for (var i = 0; i < data.length; i++) {
        var obj = data[i];
        console.log(obj.properties[0].ID);
    }
});
Run Code Online (Sandbox Code Playgroud)

这是数据的一个小样本:

"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "ID": 1, "Name": "ABC Cleaner" }, "geometry": { "type": "Point", "coordinates": [ [ [ [ 46.879682, -110.362566 ] } },
{ "type": "Feature", "properties": { "ID": 2, "Name": "Rapid X Cleaner" }, "geometry": { "type": "Point", "coordinates": [ 46.882224, -110.350167] } },
{ "type": "Feature", "properties": { "ID": 3, "Name": "Ace Cleaner" }, "geometry": { "type": "Point", "coordinates": [ 46.885817, -110.338966 ] } } ...
Run Code Online (Sandbox Code Playgroud)

例如,如果我想打印所有IDName属性,我该怎么做?

Pau*_*ald 5

您需要在循环中使用以下代码

$(document).ready(function() {
  $.getJSON('http://f.cl.ly/items/2k3d2Y3X1m0c3f0l3W3f/sample.json',
    function(data) {
      var result = data.objects.myData.geometries;
      for (var i = 0; i < result.length; i++) {
        alert(result[i].properties.ID)
      }
    });
})
Run Code Online (Sandbox Code Playgroud)