Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added oneshot callback (trigger, then stop) when location is found #343

Open
wants to merge 4 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ The control is [available from JsDelivr CDN](https://www.jsdelivr.com/projects/l

#### Add the JavaScript and CSS files

Then include the CSS and JavaScript files.
Then include the CSS and JavaScript files.

##### With CDN

Expand Down Expand Up @@ -78,6 +78,7 @@ Possible options are listed in the following table. More details are [in the cod
<!-- prettier-ignore-start -->
| Option | Type | Description | Default |
|------------|-----------|-------------------|----------|
| `callback` | `function` | function([lat,lng]) to be called when location is found. if this is set, `stop()` is called when location is first found. | `null` |
| `position` | `string` | Position of the control | `topleft` |
| `layer` | [`ILayer`](http://leafletjs.com/reference.html#ilayer) | The layer that the user's location should be drawn on. | a new layer |
| `setView` | `boolean` or `string` | Set the map view (zoom and pan) to the user's location as it updates. Options are `false`, `'once'`, `'always'`, `'untilPan'`, or `'untilPanOrZoom'` | `'untilPanOrZoom'` |
Expand Down
24 changes: 22 additions & 2 deletions src/L.Control.Locate.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ You can find the project at: https://github.com/domoritz/leaflet-locatecontrol

const LocateControl = L.Control.extend({
options: {
/** function([lat,lng]) to be called when location is found.
if this is set, stop() is called when location is first found. */
valerino marked this conversation as resolved.
Show resolved Hide resolved
callback: null,
/** Position of the control */
position: "topleft",
/** The layer that the user's location should be drawn on. By default creates a new layer. */
Expand Down Expand Up @@ -347,6 +350,12 @@ You can find the project at: https://github.com/domoritz/leaflet-locatecontrol
}
}

this._loc_callback=null
if (options.callback) {
// set location callback
valerino marked this conversation as resolved.
Show resolved Hide resolved
this._loc_callback=options.callback
}

// extend the follow marker style and circle from the normal style
this.options.followMarkerStyle = L.extend({}, this.options.markerStyle, this.options.followMarkerStyle);
this.options.followCircleStyle = L.extend({}, this.options.circleStyle, this.options.followCircleStyle);
Expand Down Expand Up @@ -756,7 +765,7 @@ You can find the project at: https://github.com/domoritz/leaflet-locatecontrol
/**
* Stores the received event and updates the marker.
*/
_onLocationFound(e) {
async _onLocationFound(e) {
valerino marked this conversation as resolved.
Show resolved Hide resolved
// no need to do anything if the location has not changed
if (this._event && this._event.latlng.lat === e.latlng.lat && this._event.latlng.lng === e.latlng.lng && this._event.accuracy === e.accuracy) {
return;
Expand All @@ -768,9 +777,12 @@ You can find the project at: https://github.com/domoritz/leaflet-locatecontrol
}

this._event = e;

this._drawMarker();
this._updateContainerStyle();
if (this._loc_callback && this._active) {
// call the location callback
this._loc_callback([e.latlng.lat, e.latlng.lng])
valerino marked this conversation as resolved.
Show resolved Hide resolved
}

switch (this.options.setView) {
case "once":
Expand All @@ -797,6 +809,10 @@ You can find the project at: https://github.com/domoritz/leaflet-locatecontrol
}

this._justClicked = false;
if (this.callback && !this.options.flyTo) {
this.stop()
}

},

/**
Expand Down Expand Up @@ -839,6 +855,10 @@ You can find the project at: https://github.com/domoritz/leaflet-locatecontrol
this._drawMarker();
}
}

if (this._loc_callback) {
this.stop()
}
},

/**
Expand Down