最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Loop Through GeoJSON Properties - Stack Overflow

programmeradmin0浏览0评论

I am trying to loop through a specific property from a GeoJSON feature, but having some troubles. 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?

I am trying to loop through a specific property from a GeoJSON feature, but having some troubles. 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?

Share Improve this question asked May 8, 2015 at 22:23 kaoscifykaoscify 1,7537 gold badges38 silver badges76 bronze badges 13
  • let me know if the answer below is providing you with the correct output @kaoscify – Paul Fitzgerald Commented May 8, 2015 at 22:33
  • @PaulFitzgerald — Thanks for your answer. I tried it, but they don't seem to print in the console. They should be printing as soon as the page loads, correct? – kaoscify Commented May 8, 2015 at 22:35
  • try the new updated code and then if you see the word 'kaoscify' in the console it may need to be changed. let me know @kaoscify – Paul Fitzgerald Commented May 8, 2015 at 22:36
  • @PaulFitzgerald — What you did makes sense, but the word 'kaoscify' did not show up. I am basically using $.getJSON to put this data on a Google Map using their API, and then would like to loop through the GeoJSON properties. If I do alert("Hello");, the alert tag shows up. – kaoscify Commented May 8, 2015 at 22:39
  • if you put the alert tag in the loop? That is definitely the correct syntax that you need to use, so if the getJSON is successful it will return what you are looking for – Paul Fitzgerald Commented May 8, 2015 at 22:41
 |  Show 8 more ments

1 Answer 1

Reset to default 5

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)
      }
    });
})
发布评论

评论列表(0)

  1. 暂无评论