My goal is to set the content of the sidebar depending on what GeoJSON feature was clicked on.
For example when clicked on polygon A the content of the sidebar is: "This is A" and when clicked on polygon B the content of the sidebar is: "This is B"
my approach to the problem (SOLVED):
function for click event to select GeoJSON feature:
function onEachFeature(feature, layer) {
layer.on({
mouseover: highlightFeature,
mouseout: resetHighlight,
click: openSidebar
});
}function to open sidebar:
function openSidebar(e) {
sidebar.toggle();
if (e.target.feature.properties.name == "House 24") {
sidebar.setContent('this is House 24');
}
}loaded GeoJSON:
var my_geojson = L.geoJson(my_geojson,{
style: myStyle,
onEachFeature: onEachFeature
}).addTo(map);
Answer
Might just need e.target or e.layer before feature.properties.name - console.log is your friend here and will likely answer your question.
No comments:
Post a Comment