From: Anton Khorev Date: Mon, 12 Aug 2024 01:31:19 +0000 (+0300) Subject: Reverse-geocode when coordinates are passed to endpoint.setValue X-Git-Tag: live~175^2~4 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/ce42b685b2084fd34270756c88db3d9ff401c2be?ds=sidebyside;hp=bb9a8958fb5bbe630da5c4bfd592a96b259df5b3 Reverse-geocode when coordinates are passed to endpoint.setValue --- 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)