onEachFeature 未被调用
onEachFeature is not called
我正在尝试添加一些具有特定样式和弹出窗口的标记,但如果我使用 PointToLayer 函数,则不会调用 onEachFeature。所以我无法添加弹出窗口。
如果我只使用 onEachFeature,我可以使用 console.log(feature)
但是我不能显示标记。如果我使用 pointToLayer,则不会调用 onEachFeature。
var json_chambre = L.geoJson(response, {
pointToLayer: function(feature, latlng) {
var markerCh = L.circleMarker(latlng, {
radius: 5,
fillColor: "#ff7800",
color: "#000",
weight: 1,
opacity: 1,
fillOpacity: 0.8
});
chambre_pit.addLayer(markerCh);
},
onEachFeature: function(feature, layer) {
console.log(feature);
}
});
没有错误,只是我不能同时有弹出窗口和样式。
让我引用 Leaflet reference for the pointToLayer
callback option:
A Function
defining how GeoJSON points spawn Leaflet layers. It is internally called when data is added, passing the GeoJSON point feature and its LatLng
. The default is to spawn a default Marker
:
function(geoJsonPoint, latlng) {
return L.marker(latlng);
}
请注意这与您的代码有何不同? pointToLayer
回调 returns L.Marker
的实例,它被添加到 L.GeoJson
实例(它是L.LayerGroup
).
由于您没有 return 标记(或图层)实例,因此 L.GeoJson
实例最终为空,并且 onEachFeature
循环遍历总共零个特征+图层对.
另请注意,您不需要在 onEachFeature
步骤附加弹出窗口,即:
var json_chambre = L.geoJson(response, {
pointToLayer: function(feature, latlng) {
var markerCh = L.circleMarker(latlng, {
radius: 5,
fillColor: "#ff7800",
color: "#000",
weight: 1,
opacity: 1,
fillOpacity: 0.8
});
markerCh.bindPopup(feature.properties.label); // Or whatever
return markerCh; // And do return the marker so it gets added to json_chambre.
}
});
我正在尝试添加一些具有特定样式和弹出窗口的标记,但如果我使用 PointToLayer 函数,则不会调用 onEachFeature。所以我无法添加弹出窗口。
如果我只使用 onEachFeature,我可以使用 console.log(feature)
但是我不能显示标记。如果我使用 pointToLayer,则不会调用 onEachFeature。
var json_chambre = L.geoJson(response, {
pointToLayer: function(feature, latlng) {
var markerCh = L.circleMarker(latlng, {
radius: 5,
fillColor: "#ff7800",
color: "#000",
weight: 1,
opacity: 1,
fillOpacity: 0.8
});
chambre_pit.addLayer(markerCh);
},
onEachFeature: function(feature, layer) {
console.log(feature);
}
});
没有错误,只是我不能同时有弹出窗口和样式。
让我引用 Leaflet reference for the pointToLayer
callback option:
A
Function
defining how GeoJSON points spawn Leaflet layers. It is internally called when data is added, passing the GeoJSON point feature and itsLatLng
. The default is to spawn a defaultMarker
:function(geoJsonPoint, latlng) { return L.marker(latlng); }
请注意这与您的代码有何不同? pointToLayer
回调 returns L.Marker
的实例,它被添加到 L.GeoJson
实例(它是L.LayerGroup
).
由于您没有 return 标记(或图层)实例,因此 L.GeoJson
实例最终为空,并且 onEachFeature
循环遍历总共零个特征+图层对.
另请注意,您不需要在 onEachFeature
步骤附加弹出窗口,即:
var json_chambre = L.geoJson(response, {
pointToLayer: function(feature, latlng) {
var markerCh = L.circleMarker(latlng, {
radius: 5,
fillColor: "#ff7800",
color: "#000",
weight: 1,
opacity: 1,
fillOpacity: 0.8
});
markerCh.bindPopup(feature.properties.label); // Or whatever
return markerCh; // And do return the marker so it gets added to json_chambre.
}
});