leaflet javascript for loop through geojson
leaflet javascript for loop through geojson
我有一个 url,其中包含我在网络地图中显示的简单点要素 geojson。
{"type": "FeatureCollection", "crs": {"type": "name", "properties": {"name": "EPSG:4326"}}, "features": [{"type": "Feature", "properties": {"name": "WORK", "pk": "3"}, "geometry": {"type": "Point", "coordinates": [-74.66645479202272, 40.79366860930495]}}, {"type": "Feature", "properties": {"name": "Great Swamp", "pk": "5"}, "geometry": {"type": "Point", "coordinates": [-74.47975158691408, 40.71239442660529]}}, {"type": "Feature", "properties": {"name": "stupid abid", "pk": "1"}, "geometry": {"type": "Point", "coordinates": [-74.25096586314858, 40.64616085364607]}}, {"type": "Feature", "properties": {"name": "Rutland", "pk": "2"}, "geometry": {"type": "Point", "coordinates": [-72.97393798828126, 43.61022814178643]}}, {"type": "Feature", "properties": {"name": "Sota", "pk": "4"}, "geometry": {"type": "Point", "coordinates": [-94.52636718750001, 46.46813299215556]}}]}
在 js 中
我只是用
var incidences = new L.GeoJSON.AJAX("http://127.0.0.1:8000/incidence_data/")
incidences.addTo(map);
并正确添加。
我知道有一个 onEachFeature 选项,它基本上充当遍历每个功能的 for 循环,但我想知道如何使用 for 循环处理 incidences 变量并打印出每个名称
for (var x in incidences.features)
console.log(incidences.features[x].properties.name);
什么都没有显示
console.log(发生率)
e {urls: Array(1), ajaxParams: {…}, _layers: {…}, options: {…}, _events: {…}, …}
ajaxParams
:
{dataType: "json", callbackParam: "callback", local: false, middleware: ƒ}
options
:
{}
urls
:
["http://127.0.0.1:8000/incidence_data/"]
_events
:
{data:loaded: Array(1), data:progress: Array(1)}
_firingCount
:
0
_initHooksCalled
:
true
_layers
:
{1334: e, 1336: e, 1337: e, 1338: e, 1339: e}
_leaflet_id
:
1335
__proto__
:
e
I want to know how to use a for loop to on the incidences variable
你不能。
在您的代码中,incidences
是 L.GeoJSON.AJAX
的一个实例。由于 class 继承,它也是 L.GeoJSON
, L.FeatureGroup
, and L.LayerGroup
.
的一个实例
None classes 实现了 iterator protocol, therefore it's not possible to use a for..of
loop. And if you try to use a for..in
loop,它将遍历实例的 方法和属性 ,而不是通过 GeoJSON功能。
在 Leaflet 中做您想做的事情的规范方法是在某处使用 getLayers()
method of L.LayerGroup
(which, because of class inheritance, is also available in L.FeatureGroup
, L.GeoJSON
and L.GeoJSON.AJAX
), store the resulting Array
,然后遍历它:
var incidenceGroup = new L.GeoJSON.AJAX("http://127.0.0.1:8000/incidence_data/")
/* ...wait for it to load with whatever method... */
var incidenceLayers = incidenceGroup.getLayers(); // incidenceLayers will be an Array
for (var incidence of incidenceLayers) { /* do stuff */ }
// Or also a more classical for(;;) loop:
for (var i, l=incidenceLayers.length; i<l; i++) {
var incidence = incidenceLayers[i];
/* do stuff */
}
我有一个 url,其中包含我在网络地图中显示的简单点要素 geojson。
{"type": "FeatureCollection", "crs": {"type": "name", "properties": {"name": "EPSG:4326"}}, "features": [{"type": "Feature", "properties": {"name": "WORK", "pk": "3"}, "geometry": {"type": "Point", "coordinates": [-74.66645479202272, 40.79366860930495]}}, {"type": "Feature", "properties": {"name": "Great Swamp", "pk": "5"}, "geometry": {"type": "Point", "coordinates": [-74.47975158691408, 40.71239442660529]}}, {"type": "Feature", "properties": {"name": "stupid abid", "pk": "1"}, "geometry": {"type": "Point", "coordinates": [-74.25096586314858, 40.64616085364607]}}, {"type": "Feature", "properties": {"name": "Rutland", "pk": "2"}, "geometry": {"type": "Point", "coordinates": [-72.97393798828126, 43.61022814178643]}}, {"type": "Feature", "properties": {"name": "Sota", "pk": "4"}, "geometry": {"type": "Point", "coordinates": [-94.52636718750001, 46.46813299215556]}}]}
在 js 中
我只是用
var incidences = new L.GeoJSON.AJAX("http://127.0.0.1:8000/incidence_data/")
incidences.addTo(map);
并正确添加。
我知道有一个 onEachFeature 选项,它基本上充当遍历每个功能的 for 循环,但我想知道如何使用 for 循环处理 incidences 变量并打印出每个名称
for (var x in incidences.features)
console.log(incidences.features[x].properties.name);
什么都没有显示
console.log(发生率)
e {urls: Array(1), ajaxParams: {…}, _layers: {…}, options: {…}, _events: {…}, …}
ajaxParams
:
{dataType: "json", callbackParam: "callback", local: false, middleware: ƒ}
options
:
{}
urls
:
["http://127.0.0.1:8000/incidence_data/"]
_events
:
{data:loaded: Array(1), data:progress: Array(1)}
_firingCount
:
0
_initHooksCalled
:
true
_layers
:
{1334: e, 1336: e, 1337: e, 1338: e, 1339: e}
_leaflet_id
:
1335
__proto__
:
e
I want to know how to use a for loop to on the incidences variable
你不能。
在您的代码中,incidences
是 L.GeoJSON.AJAX
的一个实例。由于 class 继承,它也是 L.GeoJSON
, L.FeatureGroup
, and L.LayerGroup
.
None classes 实现了 iterator protocol, therefore it's not possible to use a for..of
loop. And if you try to use a for..in
loop,它将遍历实例的 方法和属性 ,而不是通过 GeoJSON功能。
在 Leaflet 中做您想做的事情的规范方法是在某处使用 getLayers()
method of L.LayerGroup
(which, because of class inheritance, is also available in L.FeatureGroup
, L.GeoJSON
and L.GeoJSON.AJAX
), store the resulting Array
,然后遍历它:
var incidenceGroup = new L.GeoJSON.AJAX("http://127.0.0.1:8000/incidence_data/")
/* ...wait for it to load with whatever method... */
var incidenceLayers = incidenceGroup.getLayers(); // incidenceLayers will be an Array
for (var incidence of incidenceLayers) { /* do stuff */ }
// Or also a more classical for(;;) loop:
for (var i, l=incidenceLayers.length; i<l; i++) {
var incidence = incidenceLayers[i];
/* do stuff */
}