use nominatim for geocoding

This commit is contained in:
vcoppe
2024-09-06 15:49:12 +02:00
parent b343123ca6
commit 39e6532c26

View File

@@ -108,15 +108,40 @@
); );
if (geocoder) { if (geocoder) {
newMap.addControl( let geocoder = new MapboxGeocoder({
new MapboxGeocoder({ mapboxgl: mapboxgl,
accessToken: mapboxgl.accessToken, enableEventLogging: false,
mapboxgl: mapboxgl, collapsed: true,
collapsed: true, flyTo: fitBoundsOptions,
flyTo: fitBoundsOptions, language,
language localGeocoder: () => [],
}) localGeocoderOnly: true,
); externalGeocoder: (query: string) =>
fetch(
`https://nominatim.openstreetmap.org/search?format=json&q=${query}&limit=5&accept-language=${language}`
)
.then((response) => response.json())
.then((data) => {
return data.map((result: any) => {
return {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [result.lon, result.lat]
},
place_name: result.display_name
};
});
})
});
let onKeyDown = geocoder._onKeyDown;
geocoder._onKeyDown = (e: KeyboardEvent) => {
// Trigger search on Enter key only
if (e.key === 'Enter') {
onKeyDown.apply(geocoder, [{ target: geocoder._inputEl }]);
}
};
newMap.addControl(geocoder);
} }
if (geolocate) { if (geolocate) {