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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.sass-cache
bower_components/*
node_modules/*
.vscode
.DS_Store

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 |
|------------|-----------|-------------------|----------|
| `oneshot_callback` | `function` | function([lat,lng]) to be called *only once* when location is found. if this is set, `stop()` is called automatically when a 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
21 changes: 20 additions & 1 deletion 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,11 @@ You can find the project at: https://github.com/domoritz/leaflet-locatecontrol
}
}

this._loc_callback=null
if (options.oneshot_callback) {
this._loc_callback=options.oneshot_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 @@ -768,9 +776,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)
}

switch (this.options.setView) {
case "once":
Expand All @@ -797,6 +808,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 +854,10 @@ You can find the project at: https://github.com/domoritz/leaflet-locatecontrol
this._drawMarker();
}
}

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

/**
Expand Down