Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions public/lib/internal/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const defaultLanguage = "de";
const countDisplayedResults = 7;
const photonBaseUrl = "https://photon.komoot.io/";
const forwardGeocodeUrl = `${photonBaseUrl}/api`;
const reverseGeocodeUrl = `${photonBaseUrl}/reverse`;

/**
* Convert Photon response to text string
Expand Down Expand Up @@ -128,9 +129,25 @@ const forwardGeocode = (config) => {
return fetch(url).then(processPhotonResponse);
};

/**
* Callback when user enters a coordinates in search input
*/
const reverseGeocode = (config) => {
const { query } = config;
const [latitude, longitude] = query;

const url = new URL(reverseGeocodeUrl);
const params = url.searchParams;
params.append("lon", longitude);
params.append("lat", latitude);

return fetch(url).then(processPhotonResponse);
};

export function createSearchControl(mapInstance) {
const geocoderApi = {
forwardGeocode: forwardGeocode,
reverseGeocode: reverseGeocode,
};

const options = {
Expand All @@ -145,6 +162,9 @@ export function createSearchControl(mapInstance) {
// this must be set to false to avoid unwanted behaviour
// see: https://github.com/maplibre/maplibre-gl-geocoder/issues/287
showResultMarkers: false,

// when user enters coordinate, reverseGeocode callback is triggered
reverseGeocode: true,
};
return new MaplibreGeocoder(geocoderApi, options);
}
Loading