简体   繁体   English

遍历GeoJSON属性

[英]Loop Through GeoJSON Properties

I am trying to loop through a specific property from a GeoJSON feature, but having some troubles. 我正在尝试从GeoJSON功能中遍历特定属性,但遇到了一些麻烦。 Here is how I am trying to achieving it on my local server: 这是我尝试在本地服务器上实现的方法:

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

Here's a small sample of the data: 这是数据的一小部分样本:

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

For example, if I want to print all the ID or Name properties, how would I do that? 例如,如果我要打印所有IDName属性,我该怎么做?

You need to use the following code in your loop 您需要在循环中使用以下代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM