feat: add locatelocationfound event#395
Conversation
d1d6c54 to
9ee16b5
Compare
| // Fire event with location data and control reference | ||
| this._map.fire("locatelocationfound", { | ||
| latlng: e.latlng, | ||
| accuracy: e.accuracy, | ||
| altitude: e.altitude, | ||
| altitudeAccuracy: e.altitudeAccuracy, | ||
| heading: e.heading, | ||
| speed: e.speed, | ||
| timestamp: e.timestamp, | ||
| bounds: e.bounds, | ||
| control: this | ||
| }); |
There was a problem hiding this comment.
Would this work?
| // Fire event with location data and control reference | |
| this._map.fire("locatelocationfound", { | |
| latlng: e.latlng, | |
| accuracy: e.accuracy, | |
| altitude: e.altitude, | |
| altitudeAccuracy: e.altitudeAccuracy, | |
| heading: e.heading, | |
| speed: e.speed, | |
| timestamp: e.timestamp, | |
| bounds: e.bounds, | |
| control: this | |
| }); | |
| // Fire event with location data and control reference | |
| this._map.fire("locatelocationfound", { | |
| ...e, | |
| control: this | |
| }); |
There was a problem hiding this comment.
Wow. That's much more elegant and future-proof - if Leaflet ever adds new properties to the locationfound event, they will be automatically passed through. Just changed it.
| /** | ||
| * Event fired when a location is found by the locate control. | ||
| */ | ||
| export interface LocateLocationFoundEvent { |
There was a problem hiding this comment.
Is this type somewhere in leaflet as well? Would be nice not to redeclare.
There was a problem hiding this comment.
No, LocateLocationFoundEvent doesn't exist in Leaflet. But I just changed it to extends LocationEvent - so we only declare what's actually new (the control property) and inherit everything else from Leaflet's existing type. Much cleaner, just like the ...e spread in the implementation.
9ee16b5 to
8cae044
Compare
Adds a new 'locatelocationfound' event fired on the map when a location is successfully found. The event includes all geolocation data plus a reference to the control instance. This follows Leaflet's event patterns and enables use cases like one-shot location queries, custom zoom behavior, geofencing, and analytics without requiring callback options. Event data includes: - latlng, accuracy, bounds (location data) - altitude, altitudeAccuracy, heading, speed, timestamp (extended data) - control (reference to the LocateControl instance)
8cae044 to
780ce25
Compare
|
Just removed the |
I looked at PR #343, which has been open since 2023. The implementation had issues, but the use case is valid. This PR here takes a different approach - using an event instead of a callback option - which is more flexible and consistent with Leaflet's patterns.
Why is this useful?
Some practical scenarios I can imagine would need access to the control when location is found:
Why add it to the plugin?
"Can't you just use
map.locate()directly without the plugin?"Yes, but the plugin provides UI, marker rendering, accuracy circles, compass, zoom behavior, popups, error handling, and visual states. Most apps use the plugin for this functionality.
"Then just listen to the native
locationfoundevent?"You can:
But this requires keeping a separate control reference - extra boilerplate.
What this PR does
Adds a
locatelocationfoundevent with direct control access:This matches the existing pattern -
locateactivate,locatedeactivate, andlocationtimeoutalready pass the control instance.Changes: