maplibre-gl-js
maplibre-gl-js copied to clipboard
How to distinguish user-initiated map move events from events fired as a result of programmatic APIs?
maplibre-gl-js version: (all)
Question
I need a way to respond differently to map move/pan/pitch/bearing events depending on those events being a result of user-interaction or programmatic API calls.
Reasoning: We have a special mode where map view changes which are a result of user interactions are written to the DB. Map movements which is a result of API calls do not.
One way to implement it is to add an eventData
argument to all programmatic API calls as in:
map.setCenter(center, {originIsApi: true})
It can hold a flag which implies the event originates from an API call and not a result of user interaction.
This approach seems a little hackish thou. It requires updating every api call in the project: force an eventData
arg for each, which is only used for this reason.
It'd be so much more elegant to have this information built into the events by design. It'll save the developer the effort of modifying the entire API calls set.
The eventData
approach also involves an elevated risk for bugs; inexperienced developers could neglect providing a suitable eventData
arg, introducing very subtle bugs which are difficult to track down.
Anyone else facing a similar issue? Perhaps I'm missing a better solution...
I think I needed this a while back but ended doing something else at the end. Can't you add event handlers to the canvas? Or the map.on("click"...? Or you need something more complicated?
The problem is, the user has many ways to modify the maps. With the mouse, with touch, with keyboard (affecting pitch, bearing etc). It seems a little overwhelming to try and catch all of those different methods. Also there's always the lingering "what if I missed an event?" concern, which deters from using new mapLibre versions since those may have added new events which aren't covered by the current app codebase
Yea, I can't argue here, but I think implementing is as brittle as using it for the same reasons :-)
IDK.
Zooming out on the subject, asking "Is the user responsible for this event?"
seems an important enough question to deserve a native answer
I wonder if I can rely on the existence of event.originalEvent
, where event
is the first argument to the moveend
callback.
originalEvent
is missing if the the map view changes via API call however it seems to exist on user-interaction events, except for bearing-control widget events
This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 30 days.