我很难完成项目,因为根据点击的标记,根据通过json获得的数据无法更改侧栏中的信息。是我的代码:
基本的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)
侧边栏的html:
<div class="sidebar">
<div class="nome"></div>
<div class="imagem"></div>
<div class="descricao"></div>
</div>
我的JS:
var rotas = L.geoJSON(paradas, {
}).addTo(map);
只有它,我可以显示标记,但我无法继续,当我点击任何并更改侧边栏的信息。我知道的逻辑,但我不能实现缺乏知识:( 对于基本的疑问我很抱歉,但这次你能帮我吗?谢谢!!
答案 0 :(得分:3)
你应该添加处理onEachFeature的功能点击。假设您正在使用jquery(最简单的解决方案):
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);
});
}
在codepen上的