Merge pull request #1480 from umap-project/search-latlng

Allow to type a latlng in the search box
This commit is contained in:
Yohan Boniface 2024-01-03 20:59:11 +01:00 committed by GitHub
commit 558dabd113
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 8 deletions

14
package-lock.json generated
View file

@ -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",

View file

@ -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",

View file

@ -1224,9 +1224,47 @@ 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()
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) {