From ce42b685b2084fd34270756c88db3d9ff401c2be Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Mon, 12 Aug 2024 04:31:19 +0300 Subject: [PATCH] Reverse-geocode when coordinates are passed to endpoint.setValue --- .../javascripts/index/directions-endpoint.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/app/assets/javascripts/index/directions-endpoint.js b/app/assets/javascripts/index/directions-endpoint.js index b475b9a05..b3d03cdca 100644 --- a/app/assets/javascripts/index/directions-endpoint.js +++ b/app/assets/javascripts/index/directions-endpoint.js @@ -70,6 +70,7 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch if (latlng) { setLatLng(latlng); setInputValueFromLatLng(latlng); + getReverseGeocode(); changeCallback(); } else if (endpoint.value) { getGeocode(); @@ -96,6 +97,20 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch }); } + function getReverseGeocode() { + var reverseGeocodeUrl = OSM.NOMINATIM_URL + "reverse?lat=" + endpoint.latlng.lat + "&lon=" + endpoint.latlng.lng + "&format=json"; + + endpoint.geocodeRequest = $.getJSON(reverseGeocodeUrl, function (json) { + delete endpoint.geocodeRequest; + if (!json || !json.display_name) { + return; + } + + endpoint.value = json.display_name; + input.val(json.display_name); + }); + } + function setLatLng(ll) { input .attr("data-lat", ll.lat) -- 2.39.5