遍历 GeoJSON 属性

Loop Through GeoJSON Properties

我正在尝试从 GeoJSON 功能循环遍历特定 属性,但遇到了一些麻烦。这是我在本地服务器上尝试实现它的方式:

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

这是一小部分数据:

"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 ] } } ...

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

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

$(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)
      }
    });
})