Reputation: 581
I'm having a hard time finalizing a project because of not being able to change the information in my sidebar according to the data obtained through a json according to the clicked marker. Is my code:
basic json:
paradas = {
"type" : "FeatureCollection",
"name" : "paradas",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"features" : [
{
"type" : "Feature",
"properties" : {
"nome" : "Parada 1",
"imagem" : "<img src='1.jpg' alt='maptime logo gif' width='350px'/>",
"descricao" : "Test."
},
"geometry" : {
"type" : "Point",
"coordinates" : [
-38.55222702026367,
-3.7864725817550275
]
}
},
(... repeat json)
My html of the sidebar:
<div class="sidebar">
<div class="nome"></div>
<div class="imagem"></div>
<div class="descricao"></div>
</div>
My JS:
var rotas = L.geoJSON(paradas, {
}).addTo(map);
With just it, I can show the marker, but I can not continue, when I click on any and change the information of the sidebar. The logic I know, but I can not implement for lack of knowledge :( I'm sorry for the basic doubt, but Can you help me just this time? Thanks!!
Upvotes: 3
Views: 1467
Reputation: 1855
You should add function that handles onEachFeature click. Assuming you are using jquery (simplest solution):
var rotas = L.geoJSON(paradas, {
onEachFeature: onEachFeature
}).addTo(map);
function onEachFeature(feature, layer) {
layer.on('click', function(e) {
$(".nome").html(feature.properties.nome);
$(".imagem").html(feature.properties.imagem);
$(".descricao").html(feature.properties.descricao);
});
}
Working example on codepen: https://codepen.io/dagmara223/pen/BVBGKG
Upvotes: 6