Skip to content

feat: add locatelocationfound event#395

Merged
KristjanESPERANTO merged 1 commit intodomoritz:gh-pagesfrom
KristjanESPERANTO:locatelocationfound
Jan 29, 2026
Merged

feat: add locatelocationfound event#395
KristjanESPERANTO merged 1 commit intodomoritz:gh-pagesfrom
KristjanESPERANTO:locatelocationfound

Conversation

@KristjanESPERANTO
Copy link
Copy Markdown
Collaborator

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:

  • One-shot location - Get user location once, then stop (e.g., "show nearby restaurants")
  • Custom zoom - Fit bounds to include user + target marker (urb4nc4rl's use case from added oneshot callback (trigger, then stop) when location is found #343)
  • Geofencing - Check if user is in delivery area
  • Analytics - Track accuracy and behavior
  • Form autofill - Reverse geocode address

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 locationfound event?"

You can:

const lc = L.control.locate().addTo(map);
map.on('locationfound', (e) => {
  lc.stop();
});

But this requires keeping a separate control reference - extra boilerplate.

What this PR does

Adds a locatelocationfound event with direct control access:

map.on('locatelocationfound', (e) => {
  console.log(e.latlng, e.accuracy);
  e.control.stop(); // No separate reference needed
});

This matches the existing pattern - locateactivate, locatedeactivate, and locationtimeout already pass the control instance.

Changes:

  • New event with TypeScript types
  • Tests added
  • Docs updated

Comment thread src/L.Control.Locate.js
Comment on lines +885 to +896
// 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
});
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this work?

Suggested change
// 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
});

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/L.Control.Locate.d.ts Outdated
/**
* Event fired when a location is found by the locate control.
*/
export interface LocateLocationFoundEvent {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this type somewhere in leaflet as well? Would be nice not to redeclare.

Copy link
Copy Markdown
Collaborator Author

@KristjanESPERANTO KristjanESPERANTO Jan 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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)
@KristjanESPERANTO
Copy link
Copy Markdown
Collaborator Author

Just removed the LatLng import after your approval - not needed since the switch to extends LocationEvent - LocationEvent already includes it.

@KristjanESPERANTO KristjanESPERANTO merged commit e430c1e into domoritz:gh-pages Jan 29, 2026
1 check passed
@KristjanESPERANTO KristjanESPERANTO deleted the locatelocationfound branch January 29, 2026 07:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants