我有一个网址,其中包含我要在我的网络地图中显示的简单点要素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循环对入射变量和打印出每个名称
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
答案 0 :(得分:1)
我想知道如何在事件变量上使用for循环
你不能。
在您的代码中,incidences
是L.GeoJSON.AJAX
的实例。由于类继承,它也是L.GeoJSON
,L.FeatureGroup
和L.LayerGroup
的实例。
这些类均未实现iterator protocol,因此无法使用for..of
loop。而且,如果您尝试使用for..in
loop,它将遍历实例的方法和属性,而不是通过GeoJSON功能。
Leaflet中要做的经典方法是使用getLayers()
method of L.LayerGroup
(由于类继承,L.FeatureGroup
,L.GeoJSON
和{{ 1}}),将生成的Array
存储在某个位置,然后进行迭代:
L.GeoJSON.AJAX