Skip to content

Open Infrastructure Map, a view of infrastructure data in OpenStreetMap

Complete coordinates in search box

Russ Garrett3f4c151563bc
diff --git a/web/package-lock.json b/web/package-lock.json
index 6dad835..ba26719 100644
--- a/web/package-lock.json
+++ b/web/package-lock.json
@@ -10,6 +10,7 @@
"license": "ISC",
"dependencies": {
"@opencage/geosearch-core": "^0.1.10",
+ "geo-coordinates-parser": "^1.7.4",
"i18next": "^23.15.2",
"i18next-browser-languagedetector": "^8.0.0",
"lodash.invert": "^4.3.0",
@@ -3184,6 +3185,12 @@
"node": "^8.16.0 || ^10.6.0 || >=11.0.0"
}
},
+ "node_modules/geo-coordinates-parser": {
+ "version": "1.7.4",
+ "resolved": "https://registry.npmjs.org/geo-coordinates-parser/-/geo-coordinates-parser-1.7.4.tgz",
+ "integrity": "sha512-gVGxBW+s1csexXVMf5bIwz3TH9n4sCEglOOOqmrPk8YazUI5f79jCowKjTw05m/0h1//3+Z2m/nv8IIozgZyUw==",
+ "license": "MIT"
+ },
"node_modules/geojson-vt": {
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/geojson-vt/-/geojson-vt-4.0.2.tgz",
diff --git a/web/package.json b/web/package.json
index 53daeb4..8d2ef81 100644
--- a/web/package.json
+++ b/web/package.json
@@ -34,6 +34,7 @@
},
"dependencies": {
"@opencage/geosearch-core": "^0.1.10",
+ "geo-coordinates-parser": "^1.7.4",
"i18next": "^23.15.2",
"i18next-browser-languagedetector": "^8.0.0",
"lodash.invert": "^4.3.0",
diff --git a/web/src/geocoder.ts b/web/src/geocoder.ts
index 59adbf9..d32f571 100644
--- a/web/src/geocoder.ts
+++ b/web/src/geocoder.ts
@@ -1,10 +1,71 @@
import i18next from 'i18next'
import { el } from 'redom'
-import { IControl, LngLat, LngLatBounds, Map } from 'maplibre-gl'
+import { IControl, LngLat, LngLatBounds, Map, Marker } from 'maplibre-gl'
+import { convert as convert_coords } from 'geo-coordinates-parser'
import { OpenCageGeoSearchPlugin } from '@opencage/geosearch-core'
-import { autocomplete } from '@algolia/autocomplete-js'
+import { autocomplete, AutocompletePlugin } from '@algolia/autocomplete-js'
import '@algolia/autocomplete-theme-classic'
+function latLonSearchPlugin(map: Map): AutocompletePlugin<any, any> {
+ return {
+ getSources() {
+ let marker: Marker | null = null
+ return [
+ {
+ sourceId: 'lat-lon',
+ getItems({ query }) {
+ if (marker) {
+ marker.remove()
+ marker = null
+ }
+ if (!query) {
+ return []
+ }
+
+ let coords
+ try {
+ coords = convert_coords(query)
+ } catch {
+ return []
+ }
+
+ return [
+ {
+ label: query,
+ value: {
+ geometry: {
+ lat: coords.decimalLatitude.toFixed(5),
+ lng: coords.decimalLongitude.toFixed(5)
+ }
+ }
+ }
+ ]
+ },
+ onSelect({ item }) {
+ marker = new Marker().setLngLat([item.value.geometry.lng, item.value.geometry.lat]).addTo(map)
+
+ map.flyTo({
+ center: [item.value.geometry.lng, item.value.geometry.lat],
+ zoom: 12
+ })
+ },
+ onReset() {
+ if (marker) {
+ marker.remove()
+ marker = null
+ }
+ },
+ templates: {
+ item({ item }) {
+ return `Coordinates: ${item.label}`
+ }
+ }
+ }
+ ]
+ }
+ }
+}
+
export default class OpenInfraMapGeocoder implements IControl {
map?: Map
container?: HTMLElement
@@ -15,6 +76,7 @@ export default class OpenInfraMapGeocoder implements IControl {
autocomplete({
container: this.container,
plugins: [
+ latLonSearchPlugin(map),
OpenCageGeoSearchPlugin(
{
// This key only works on openinframap.org (and local dev).