1

My objective is to plot polygons and assign a tooltip label to each one. For some reason, my code doesn't seems to work.

poly_geojson = L.geoJson(poly_gon,{
    onEachFeature(feature, layer) {
        if (feature.properties.sourceLang == "en") {
            layer.bindTooltip("<h4>"+feature.properties.name+"</h4>")
        }
        else {
            layer.bindTooltip("<h4>"+feature.properties.name+"</h4>")
        }
    }
}).addTo(map);
3
  • Specification of onEachFeature option is wrong, it should be: onEachFeature: function(feature, layer) { … } – TomazicM Dec 19 '19 at 7:11
  • hey, i still face the same issue, the code works if i replace 'bindTooltip' with 'bindPopup'. im not sure if i am doing this right – arya Dec 19 '19 at 7:27
  • 2
    If you corrected syntax, it should work. – TomazicM Dec 19 '19 at 17:21
0

According to leaflet's documentation - after bindTooltip() - add openTooltip() Code - I added the 'options fix' from the comments:

poly_geojson = L.geoJson(poly_gon,{ onEachFeature : function(feature, layer) { if (feature.properties.sourceLang == "en") { layer.bindTooltip("<h4>"+feature.properties.name+"</h4>"); } else { layer.bindTooltip("<h4>"+feature.properties.name+"</h4>"); } } }).addTo(map);

EDIT: no need to use openTooltip() please check this codepen

2
  • 1
    Any special reason for opening all tooltips upon definition with .openTooltip()? This is not necessary unless you want to have all the tooltips open initially. Usually behavior for tooltip is to be opened upon mouse over. – TomazicM Jan 29 '20 at 13:13
  • If a codePen is avail - it'll be easier to explain. I might be wrong of course. @TomazicM – NettaB Jan 29 '20 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.