From 554352e003fe84d76362f42685c04da9a870e770 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Thu, 21 Dec 2023 12:39:51 +0100 Subject: [PATCH 1/3] Allow to type a latlng in the search box fix #1000 cf #1001 --- umap/static/umap/js/umap.controls.js | 38 +++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/umap/static/umap/js/umap.controls.js b/umap/static/umap/js/umap.controls.js index 157f32bf..ee76e440 100644 --- a/umap/static/umap/js/umap.controls.js +++ b/umap/static/umap/js/umap.controls.js @@ -1210,7 +1210,6 @@ L.U.TileLayerControl = L.Control.IconLayers.extend({ L.Control.IconLayers.prototype.setLayers.call(this, layers.slice(0, maxShown)) if (this.map.selected_tilelayer) this.setActiveLayer(this.map.selected_tilelayer) }, - }) /* Used in edit mode to define the default tilelayer */ @@ -1357,6 +1356,43 @@ L.U.Search = L.PhotonSearch.extend({ L.PhotonSearch.prototype.initialize.call(this, map, input, options) this.options.url = map.options.urls.search if (map.options.maxBounds) this.options.bbox = map.options.maxBounds.toBBoxString() + this.reverse = new L.PhotonReverse({ + handleResults: (geojson) => { + this.handleResultsWithReverse(geojson) + }, + }) + }, + + handleResultsWithReverse: function (geojson) { + const latlng = this.reverse.latlng + geojson.features.unshift({ + type: 'Feature', + geometry: { type: 'Point', coordinates: [latlng.lng, latlng.lat] }, + properties: { + name: L._('Go to "{coords}"', { coords: `${latlng.lat} ${latlng.lng}` }), + }, + }) + + this.handleResults(geojson) + }, + + search: function () { + const pattern = /^(?[-+]?\d{1,2}[.,]\d+)\s*[ ,]\s*(?[-+]?\d{1,3}[.,]\d+)$/ + if (pattern.test(this.input.value)) { + this.hide() + const { lat, lng } = pattern.exec(this.input.value).groups + const latlng = L.latLng(lat, lng) + if (latlng.isValid()) { + this.reverse.doReverse(latlng) + } else { + this.map.ui.alert({ content: 'Invalid latitude or longitude', mode: 'error' }) + } + return + } + // Only numbers, abort. + if (/^[\d .,]*$/.test(this.input.value)) return + // Do normal search + L.PhotonSearch.prototype.search.call(this) }, onBlur: function (e) { From 70ae3e4bd8e1f7b886fd8e3e4eb7530ac524c8b7 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Fri, 22 Dec 2023 11:13:03 +0100 Subject: [PATCH 2/3] Upgrade leaflet.photon to 0.9.0 This release fixes the API domain --- package-lock.json | 14 +++++++------- package.json | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index 47608090..aca9d953 100644 --- a/package-lock.json +++ b/package-lock.json @@ -31,7 +31,7 @@ "leaflet.locatecontrol": "^0.79.0", "leaflet.markercluster": "^1.5.3", "leaflet.path.drag": "0.0.6", - "leaflet.photon": "0.8.0", + "leaflet.photon": "0.9.0", "osmtogeojson": "^3.0.0-beta.3", "simple-statistics": "^7.8.3", "togpx": "^0.5.4", @@ -1465,9 +1465,9 @@ "integrity": "sha1-bZw68LnXsDJUSuFr/eaI8BYFFKA=" }, "node_modules/leaflet.photon": { - "version": "0.8.0", - "resolved": "https://registry.npmjs.org/leaflet.photon/-/leaflet.photon-0.8.0.tgz", - "integrity": "sha512-uvCPocNvRJaArW8yPm6K4bkgvoMCbCOA9tgFlQfOVw+mRmN4jbkuEFoSP0nJhj8UKIOWsRtGfOjxtzaJyZuciw==" + "version": "0.9.0", + "resolved": "https://registry.npmjs.org/leaflet.photon/-/leaflet.photon-0.9.0.tgz", + "integrity": "sha512-qLZzYqBAHvOGgf/ymj3zdknlR1zfVZTPSF02wfES3tWR0iZA48HaxpFTZ0ZLflHxsDuQf6Hl+03xwBAHHQTslA==" }, "node_modules/lebab": { "version": "3.2.1", @@ -3674,9 +3674,9 @@ "integrity": "sha1-bZw68LnXsDJUSuFr/eaI8BYFFKA=" }, "leaflet.photon": { - "version": "0.8.0", - "resolved": "https://registry.npmjs.org/leaflet.photon/-/leaflet.photon-0.8.0.tgz", - "integrity": "sha512-uvCPocNvRJaArW8yPm6K4bkgvoMCbCOA9tgFlQfOVw+mRmN4jbkuEFoSP0nJhj8UKIOWsRtGfOjxtzaJyZuciw==" + "version": "0.9.0", + "resolved": "https://registry.npmjs.org/leaflet.photon/-/leaflet.photon-0.9.0.tgz", + "integrity": "sha512-qLZzYqBAHvOGgf/ymj3zdknlR1zfVZTPSF02wfES3tWR0iZA48HaxpFTZ0ZLflHxsDuQf6Hl+03xwBAHHQTslA==" }, "lebab": { "version": "3.2.1", diff --git a/package.json b/package.json index bad0c918..7d9b551e 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,7 @@ "leaflet.locatecontrol": "^0.79.0", "leaflet.markercluster": "^1.5.3", "leaflet.path.drag": "0.0.6", - "leaflet.photon": "0.8.0", + "leaflet.photon": "0.9.0", "osmtogeojson": "^3.0.0-beta.3", "simple-statistics": "^7.8.3", "togpx": "^0.5.4", From 9e346d2d4c4ecf870a8bbcc1187fae9e11c499da Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Tue, 26 Dec 2023 12:16:50 +0100 Subject: [PATCH 3/3] search input: add a placeholder to mention typing coordinates --- umap/static/umap/js/umap.controls.js | 1 + 1 file changed, 1 insertion(+) diff --git a/umap/static/umap/js/umap.controls.js b/umap/static/umap/js/umap.controls.js index ee76e440..217b842b 100644 --- a/umap/static/umap/js/umap.controls.js +++ b/umap/static/umap/js/umap.controls.js @@ -1353,6 +1353,7 @@ L.U.StarControl = L.Control.extend({ L.U.Search = L.PhotonSearch.extend({ initialize: function (map, input, options) { + this.options.placeholder = L._('Type a place name or coordinates') L.PhotonSearch.prototype.initialize.call(this, map, input, options) this.options.url = map.options.urls.search if (map.options.maxBounds) this.options.bbox = map.options.maxBounds.toBBoxString()