From 80a1d08dc17bbbff939e6d6d3afbf1a6d2eb6905 Mon Sep 17 00:00:00 2001 From: valerino Date: Tue, 3 Oct 2023 11:45:43 +0200 Subject: [PATCH 1/4] added oneshot callback (trigger, then stop) when location is found --- README.md | 3 ++- src/L.Control.Locate.js | 24 ++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1676616..bd82b30 100644 --- a/README.md +++ b/README.md @@ -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 @@ -78,6 +78,7 @@ Possible options are listed in the following table. More details are [in the cod | 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'` | diff --git a/src/L.Control.Locate.js b/src/L.Control.Locate.js index 003d5dc..7f4bc46 100644 --- a/src/L.Control.Locate.js +++ b/src/L.Control.Locate.js @@ -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. */ + 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. */ @@ -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 + 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); @@ -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) { // 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; @@ -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]) + } switch (this.options.setView) { case "once": @@ -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() + } + }, /** @@ -839,6 +855,10 @@ You can find the project at: https://github.com/domoritz/leaflet-locatecontrol this._drawMarker(); } } + + if (this._loc_callback) { + this.stop() + } }, /** From de9d1bb3062e51186dfb7bb04853ea9c06d599cc Mon Sep 17 00:00:00 2001 From: valerino Date: Wed, 4 Oct 2023 16:08:10 +0200 Subject: [PATCH 2/4] renamed callback to "oneshot_callback", fixed typos --- README.md | 2 +- src/L.Control.Locate.js | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index bd82b30..c8fc948 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ Possible options are listed in the following table. More details are [in the cod | 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` | +| `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'` | diff --git a/src/L.Control.Locate.js b/src/L.Control.Locate.js index 7f4bc46..7b5e084 100644 --- a/src/L.Control.Locate.js +++ b/src/L.Control.Locate.js @@ -351,9 +351,8 @@ You can find the project at: https://github.com/domoritz/leaflet-locatecontrol } this._loc_callback=null - if (options.callback) { - // set location callback - this._loc_callback=options.callback + if (options.oneshot_callback) { + this._loc_callback=options.oneshot_callback } // extend the follow marker style and circle from the normal style @@ -765,7 +764,7 @@ You can find the project at: https://github.com/domoritz/leaflet-locatecontrol /** * Stores the received event and updates the marker. */ - async _onLocationFound(e) { + _onLocationFound(e) { // 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; @@ -781,7 +780,7 @@ You can find the project at: https://github.com/domoritz/leaflet-locatecontrol this._updateContainerStyle(); if (this._loc_callback && this._active) { // call the location callback - this._loc_callback([e.latlng.lat, e.latlng.lng]) + this._loc_callback(e.latlng) } switch (this.options.setView) { From c461a414f6f94d2b261aabc22aebb7efc4ec1949 Mon Sep 17 00:00:00 2001 From: valerino Date: Sun, 8 Oct 2023 13:39:13 +0200 Subject: [PATCH 3/4] added .vscode to gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 5be65a9..6760ad7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .sass-cache bower_components/* node_modules/* +.vscode + From b72d51409f4e236b3bb9b7bcbb5d04fa152e8d80 Mon Sep 17 00:00:00 2001 From: valerino Date: Sat, 14 Oct 2023 20:42:43 +0200 Subject: [PATCH 4/4] updated gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 6760ad7..bf25b78 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ bower_components/* node_modules/* .vscode +.DS_Store