diff --git a/server/scripts/index.mjs b/server/scripts/index.mjs index 448aa7e..7391d8a 100644 --- a/server/scripts/index.mjs +++ b/server/scripts/index.mjs @@ -113,7 +113,14 @@ const init = async () => { const latLon = parsedParameters.latLon ?? localStorage.getItem('latLon'); const fromGPS = localStorage.getItem('latLonFromGPS') && !loadFromParsed; - if (query && latLon && !fromGPS) { + if (parsedParameters.latLonQuery && !parsedParameters.latLon) { + const txtAddress = document.querySelector(TXT_ADDRESS_SELECTOR); + txtAddress.value = parsedParameters.latLonQuery; + const geometry = await geocodeLatLonQuery(parsedParameters.latLonQuery); + if (geometry) { + doRedirectToGeometry(geometry); + } + } else if (query && latLon && !fromGPS) { const txtAddress = document.querySelector(TXT_ADDRESS_SELECTOR); txtAddress.value = query; loadData(JSON.parse(latLon)); @@ -162,6 +169,26 @@ const init = async () => { document.querySelector('#container').addEventListener('swiped-right', () => swipeCallBack('right')); }; +const geocodeLatLonQuery = async (query) => { + try { + const data = await json('https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/find', { + data: { + text: query, + f: 'json', + }, + }); + + const loc = data.locations?.[0]; + if (loc) { + return loc.feature.geometry; + } + return null; + } catch (error) { + console.error('Geocoding failed:', error); + return null; + } +}; + const autocompleteOnSelect = async (suggestion) => { // Note: it's fine that this uses json instead of safeJson since it's infrequent and user-initiated const data = await json('https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/find', {