]> git.openstreetmap.org Git - rails.git/commitdiff
Cache last reverse geocoding result
authorAnton Khorev <tony29@yandex.ru>
Mon, 12 Aug 2024 04:15:35 +0000 (07:15 +0300)
committerAnton Khorev <tony29@yandex.ru>
Sun, 8 Sep 2024 07:38:53 +0000 (10:38 +0300)
Otherwise selecting "Directions from here" and then "Directions to here" results in two reverse-geocoding request for the starting point.

app/assets/javascripts/index/directions-endpoint.js

index 604dcc2b6c6b8ed3a0751531dec9a0f452be92ed..b9dc65748fbd84904adc7a044138b5e1c049a8ca 100644 (file)
@@ -57,17 +57,25 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch
   }
 
   endpoint.setValue = function (value) {
-    endpoint.value = value;
-    removeLatLng();
-    input.removeClass("is-invalid");
-    input.val(value);
-
     if (endpoint.geocodeRequest) endpoint.geocodeRequest.abort();
     delete endpoint.geocodeRequest;
+    input.removeClass("is-invalid");
 
     var coordinatesMatch = value.match(/^\s*([+-]?\d+(?:\.\d*)?)(?:\s+|\s*[/,]\s*)([+-]?\d+(?:\.\d*)?)\s*$/);
     var latlng = coordinatesMatch && L.latLng(coordinatesMatch[1], coordinatesMatch[2]);
 
+    if (latlng && endpoint.cachedReverseGeocode && endpoint.cachedReverseGeocode.latlng.equals(latlng)) {
+      setLatLng(latlng);
+      endpoint.value = endpoint.cachedReverseGeocode.value;
+      input.val(endpoint.value);
+      changeCallback();
+      return;
+    }
+
+    endpoint.value = value;
+    removeLatLng();
+    input.val(value);
+
     if (latlng) {
       setLatLng(latlng);
       setInputValueFromLatLng(latlng);
@@ -92,6 +100,7 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch
 
       setLatLng(L.latLng(json[0]));
 
+      endpoint.value = json[0].display_name;
       input.val(json[0].display_name);
 
       changeCallback();
@@ -99,7 +108,8 @@ 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";
+    var latlng = endpoint.latlng.clone();
+    var reverseGeocodeUrl = OSM.NOMINATIM_URL + "reverse?lat=" + latlng.lat + "&lon=" + latlng.lng + "&format=json";
 
     endpoint.geocodeRequest = $.getJSON(reverseGeocodeUrl, function (json) {
       delete endpoint.geocodeRequest;
@@ -109,6 +119,7 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch
 
       endpoint.value = json.display_name;
       input.val(json.display_name);
+      endpoint.cachedReverseGeocode = { latlng: latlng, value: endpoint.value };
     });
   }