2017-08-27 187 views
1

我正在使用Leaflet,我希望它具有這樣的功能,即當用戶單擊某個標記時,文本將顯示在地圖旁邊,並具有完整描述的內容上。一個彈出窗口已經出現在對標記的簡短描述中,但我也想要一個更長的描述。這應該是非常簡單的,除非我不知道如何正確引用標記。使用Leaflet標記單擊事件將文本添加到文檔

L.geoJSON(geojsonFeature, { 
    onEachFeature: onEachFeature 
}).bindPopup(function(layer) { 
    return layer.feature.properties.popupContent 
}).on('click', markerOnClick).addTo(map); 

function markerOnClick(e) { 
    document.getElementById("text").innerHTML = (this.options.properties.description); 
} 

所以我可以用這個 -

 function markerOnClick(e) 
{  document.getElementById("text").innerHTML=e.layer.feature.properties.description;} 

回答

0

你不這樣做,將你的方法給每個標記已經回答了我的問題。你需要用另一種方式來做到這一點:

// It will add the method on click on each layer of your GeoJson 
function onEachFeature(feature, layer) { 
    layer.on('click', function (e) { 
    document.getElementById("text").html(e.layer.feature.properties.description); 
    }); 

} 

geojson = L.geoJson(geojsonFeature, { 
    style: style, 
    onEachFeature: onEachFeature 
}).bindPopup(function(layer) { 
     return layer.feature.properties.popupContent 
}).addTo(map); 

進入function (e)你應該控制檯登錄變數e,看你是否能e.layer.feature.properties.description

訪問您的數據
相關問題