Allow to type a latlng in the search box

fix #1000
cf #1001
This commit is contained in:
Yohan Boniface 2023-12-21 12:39:51 +01:00
parent 417f1b9d08
commit 554352e003

View file

@ -1210,7 +1210,6 @@ L.U.TileLayerControl = L.Control.IconLayers.extend({
L.Control.IconLayers.prototype.setLayers.call(this, layers.slice(0, maxShown)) L.Control.IconLayers.prototype.setLayers.call(this, layers.slice(0, maxShown))
if (this.map.selected_tilelayer) this.setActiveLayer(this.map.selected_tilelayer) if (this.map.selected_tilelayer) this.setActiveLayer(this.map.selected_tilelayer)
}, },
}) })
/* Used in edit mode to define the default 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) L.PhotonSearch.prototype.initialize.call(this, map, input, options)
this.options.url = map.options.urls.search this.options.url = map.options.urls.search
if (map.options.maxBounds) this.options.bbox = map.options.maxBounds.toBBoxString() 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 = /^(?<lat>[-+]?\d{1,2}[.,]\d+)\s*[ ,]\s*(?<lng>[-+]?\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) { onBlur: function (e) {