]> git.openstreetmap.org Git - rails.git/commitdiff
Reverse-geocode when coordinates are passed to endpoint.setValue
authorAnton Khorev <tony29@yandex.ru>
Mon, 12 Aug 2024 01:31:19 +0000 (04:31 +0300)
committerAnton Khorev <tony29@yandex.ru>
Sun, 8 Sep 2024 07:38:53 +0000 (10:38 +0300)
app/assets/javascripts/index/directions-endpoint.js

index b475b9a05e728904b14b1b50bd03e6fe60a15cf8..b3d03cdca5ccc97b4a5a9bcfc5119271ad75e91f 100644 (file)
@@ -70,6 +70,7 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch
     if (latlng) {
       setLatLng(latlng);
       setInputValueFromLatLng(latlng);
     if (latlng) {
       setLatLng(latlng);
       setInputValueFromLatLng(latlng);
+      getReverseGeocode();
       changeCallback();
     } else if (endpoint.value) {
       getGeocode();
       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)
   function setLatLng(ll) {
     input
       .attr("data-lat", ll.lat)