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?
- 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 doalert("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
1 Answer
Reset to default 5You 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)
}
});
})