I'm using a geoJson layer with Leaflet.js, showing countries here.
I'm adding country labels with the following:
L.marker(layer.getBounds().getCenter(), {
icon: L.divIcon({
className: 'countryLabel',
html: feature.properties.name,
iconSize: [0, 0]
})
}).addTo(map);
The problem is, the markup this applies obstructs the mouseover in the area of each country, causing issues with the mouseover color change and clickable area.
Is there a better solution in leaflet 1.0.3 to supply labels that will not obstruct the countries clickable area?
I've tried code that uses the Leaflet.Label extension like this:
var label = new L.Label();
label.setContent(feature.properties.name);
label.setLatLng(center);
map.showLabel(label);
or
L.marker(center)
.bindLabel('test', { noHide: true })
.addTo(map);
But these cause errors; I understand that functionality from this plugin was incorporated into Leaflet.js itself after v1.
This does work, but I'd rather have straightforward labels instead of tooltips:
var marker = new L.marker(center, { opacity: 0.00 }); //opacity may be set to zero
marker.bindTooltip(feature.properties.name, { permanent: true, className: "my-label", offset: [0, 0] });
marker.addTo(map);
Any ideas would be welcome.