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) {