jupyter-widgets / ipyleaflet

A Jupyter - Leaflet.js bridge
https://ipyleaflet.readthedocs.io
MIT License
1.48k stars 363 forks source link

Mouse events do not correctly report their location when they are over a marker which contains a DivIcon. #1218

Open charliedaly opened 1 month ago

charliedaly commented 1 month ago

The following code shows the problem:

from ipyleaflet import Map, Marker, DivIcon

m = ipyleaflet.Map(center = (52, -8))

def handler(**kwargs):
    if kwargs['type'] == 'mousemove':
         print(kwargs)

icon = DivIcon(icon_size = [100, 100])

marker = Marker(location = (52, -8), icon = icon)

m.add(marker)

m.on_interaction(handler)
m

When the mouse is over the marker, it shows the coordinates of the marker rather than the mouse.

charliedaly commented 1 month ago

This happens even when the DivIcon is covered by another marker. In the following case, it's covered by a CircleMarker:

from ipyleaflet import Map, Marker, DivIcon, CircleMarker

m = Map(center = (52, -8))

def get_handler(m):
    m.coords = None
    def handler(**kwargs):
        if kwargs['type'] == 'mousemove':
            if m.coords == kwargs['coordinates']:
                print('.', end='')
            else:
                m.coords = kwargs['coordinates']
                print(kwargs)

    return handler

icon = DivIcon(icon_size = [100, 100])

marker = Marker(location = (52, -8), icon = icon)

cm = CircleMarker(location = (52, -8), radius = 80)

m.add(marker)
m.add(cm)

m.on_interaction(get_handler(m))
m

The mouse coordinates are correctly reported everywhere except where the DivIcon is.