我正在尝试从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 ] } } ...
例如,如果我要打印所有ID
或Name
属性,我该怎么做?
答案 0 :(得分:3)
您需要在循环中使用以下代码
$(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)
}
});
})