1

How can I manually set the coordinates for a tooltip in Leaflet with geoJSON?
This is how I'm adding it to the map:

layer.bindTooltip(country_name, {
    permanent: true,
    opacity: 1,
    direction: "center"
}).openTooltip();

But I want to center the tooltip or choose where to add it, rather than use the default location:
enter image description here

1 Answer 1

1

You can control position of the tooltip only for map tooltips (see https://leafletjs.com/reference.html#map-methods-for-layers-and-controls).

So if your desired tooltip coordinates would be latlng, you can set tooltip at the desired location with the following code:

map.openTooltip(country_name, latlng, {
    permanent: true,
    opacity: 1,
    direction: "center"
});

Here is working JSFiddle: https://jsfiddle.net/TomazicM/hq0jd3xz/

6
  • This doesn't work, the tooltip placement is still the same as the image above when using: map.openTooltip(country_name, [11, 12], { May 5 at 12:48
  • What do you mean by "still the same"? With the coordinates above tooltip opens somewhere in Nigeria.
    – TomazicM
    May 5 at 12:55
  • Sorry, I meant to put [-11, -50] as the manual coordinates, a location in approximately central Brazil, but the tooltip still appears in the same place as the above screenshot, near the bottom of the country. I'm also finding that by adding manual coordinates like you said above, it's causing the tooltip to flicker between showing and not showing, when navigating the map. Do you know why this may be happening? May 6 at 8:43
  • As for tooltip still appearing at the same position, that's probably because you left the old definition of the tooltip in the code. You should remove it. And about the flickering, that's probably consequence of how and where you define it in your code.
    – TomazicM
    May 6 at 9:13
  • I haven't "defined" the tooltip anywhere else in the code, I'm not sure why it's behaving this way. But I have found that Leaflet's divIcon is better suited to what I'm looking for. May 6 at 10:52

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.