question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Tooltip for Polygon is not inside its bounds when using 'center' direction

See original GitHub issue
  • I’ve looked at the documentation to make sure the behavior is documented and expected
  • I’m sure this is a Leaflet code issue, not an issue with my own code nor with the framework I’m using (Cordova, Ionic, Angular, React…)
  • I’ve searched through the issues to make sure it’s not yet reported

Steps to reproduce Steps to reproduce the behavior:

  • Add a U-shaped GeoJSON polygon to the map. Example GeoJSON:
{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              24.015595116635151,
              49.838027167808256
            ],
            [
              24.015595115971802,
              49.837969321590116
            ],
            [
              24.015604012716384,
              49.837969321547092
            ],
            [
              24.015604012758285,
              49.837972917830491
            ],
            [
              24.015609573224058,
              49.837972917803256
            ],
            [
              24.015609573181745,
              49.837969321519864
            ],
            [
              24.015621250159008,
              49.837969321461806
            ],
            [
              24.0156212501277,
              49.837966714156337
            ],
            [
              24.015867231200939,
              49.837966712659977
            ],
            [
              24.015867231249167,
              49.837969536516852
            ],
            [
              24.015886095127993,
              49.837969536380541
            ],
            [
              24.015886095415542,
              49.837985997593371
            ],
            [
              24.015857500712581,
              49.837985997798782
            ],
            [
              24.015857500730792,
              49.837987076683795
            ],
            [
              24.015781947880019,
              49.8379870771926
            ],
            [
              24.015781947863491,
              49.837985998307587
            ],
            [
              24.015655169209623,
              49.837985999050723
            ],
            [
              24.015655169666566,
              49.838021961884472
            ],
            [
              24.015719112881367,
              49.838021961526984
            ],
            [
              24.015719112755278,
              49.838012970818561
            ],
            [
              24.015735796372667,
              49.83801297071949
            ],
            [
              24.015735796501851,
              49.838021961427906
            ],
            [
              24.015781948414457,
              49.838021961141322
            ],
            [
              24.01578194839793,
              49.838020882256323
            ],
            [
              24.015853289244387,
              49.838020881777183
            ],
            [
              24.015853289262505,
              49.838021960662189
            ],
            [
              24.015886096043758,
              49.838021960427106
            ],
            [
              24.015886096358646,
              49.83803998679744
            ],
            [
              24.015621251007467,
              49.838039988430111
            ],
            [
              24.015621250853531,
              49.838027167679954
            ],
            [
              24.015595116635151,
              49.838027167808256
            ]
          ]
        ]
      },
      "properties": {
        "id": "UNIT22",
        "name": "4G120"
      }
    }
  ]
}
  • Use bindTooltip in the onEachFeature when creating the layer for the feature to set a permanent tooltip for it
    if (feature.properties.name) {
        layer.bindTooltip(feature.properties.name, {
            permanent: true,
            direction: 'center'
        });
    }

Expected behavior

The tooltip is displayed inside polygon bounds.

Current behavior

The tooltip is displayed outside polygon bounds, but seems like it’s in the center of its bbox.

Environment

  • Leaflet version: 1.7.1 (using "@asymmetrik/ngx-leaflet": "^8.1.0")
  • Browser (with version): Chrome 89.0.4389.114
  • OS/Platform (with version): Windows 10 build 19042.906

Additional context

I expected it to behave the same way as a Polyline (using centroid), but it didn’t. I also tried setting the [lat, lon] after bindTooltip with the coordinates returned by @turf/centroid with no success.

Minimal example reproducing the issue

https://plnkr.co/edit/lz2wHJmUHJ1jhsX2

  • this example is as simple as possible
  • this example does not rely on any third party code

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
johnd0ecommented, Apr 14, 2021

See the code: https://github.com/Leaflet/Leaflet/blob/0f904a515879fcd08f69b7f51799ee7f18f23fd8/src/layer/DivOverlay.js#L176-L184

To change behavior you need to overwrite one of those methods, e.g. getCenter, using include (or see tutorial):

L.Polygon.include({
  getCenter: function () {
    // ...
  }
});
0reactions
YaroslavKormushyncommented, Apr 14, 2021

@johnd0e Thanks for the answer! Feel free to close the issue if you need a separate one for the issue you’ve found.

Read more comments on GitHub >

github_iconTop Results From Across the Web

tooltip's aren't "centered" despite direction: center being set
My issue is that the tooltip does not appear to be centered around the marker, in spite of direction: 'center' being set. –...
Read more >
tooltip binded to polygon disappear when zoom in or zoom out
The tooltip does a good job of choosing coordinates near the center of the polygon, but still inside the polygon -- except it...
Read more >
Largest polygon inside a GeoJSON multipolygon
I have a Multipolygon and when adding a tootltip to it with Leaflet, the tooltip is attached to the first polygon or a...
Read more >
L.Tooltip - WRLD3D
Tooltip. Used to display small texts on top of map layers. Usage example. marker.bindTooltip("my tooltip text").openTooltip();. Note about tooltip offset.
Read more >
Tooltips - React Leaflet
'Click this Circle to change the Tooltip text'. : `Circle click: ${clickedCount}`. return (. <Circle. center={center}. eventHandlers={eventHandlers}.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found