globe.gl icon indicating copy to clipboard operation
globe.gl copied to clipboard

get the underlaying Polygon id (hexBindata ???) when .onHexPolygonClick((polygon, event, coords)

Open pburel opened this issue 1 year ago • 1 comments

Hello,

How to get the underlaying Polygon/cell id (not the whole cluser) (hexBindata ???) when .onHexPolygonClick((polygon, event, coords)

I need to center a html marker from the middle of this cell of polygon , from the base of the svg ...

How html markers may be zoomed or unzoomed depending on globe/world zoom factor

Thanks

pburel avatar Apr 04 '25 10:04 pburel

@pburel thanks for reaching out.

Using just the arguments from the onHexPolygonClick you can't derive exactly the center of the cell that was clicked, the closest you'll get is the exact coordinates of the click (the third argument: coords).

But, using the h3-js module you can easily reverse calculate this cell center.

For example:

.onHexPolygonClick((polygon, event, coords) => {
  const h3Idx = h3.latLngToCell(coords.lat, coords.lng, 3); // make sure the resolution level matches what's specified in .hexPolygonResolution
  const cellCenter = h3.cellToLatLng(h3Idx); // returns a [lat, lng] array
})

vasturiano avatar Apr 04 '25 15:04 vasturiano