]> git.openstreetmap.org Git - rails.git/blobdiff - app/assets/javascripts/index/directions-endpoint.js
Merge pull request #5591 from AntonKhorev/api-element-resources--index-paths
[rails.git] / app / assets / javascripts / index / directions-endpoint.js
index b9dc65748fbd84904adc7a044138b5e1c049a8ca..30d499ad731707b404dc1ca3448af2e215cfb7d7 100644 (file)
@@ -34,7 +34,7 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch
   };
 
   function markerDragListener(e) {
-    var latlng = convertLatLngToZoomPrecision(e.target.getLatLng());
+    const latlng = L.latLng(OSM.cropLocation(e.target.getLatLng(), map.getZoom()));
 
     if (endpoint.geocodeRequest) endpoint.geocodeRequest.abort();
     delete endpoint.geocodeRequest;
@@ -66,7 +66,12 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch
 
     if (latlng && endpoint.cachedReverseGeocode && endpoint.cachedReverseGeocode.latlng.equals(latlng)) {
       setLatLng(latlng);
-      endpoint.value = endpoint.cachedReverseGeocode.value;
+      if (endpoint.cachedReverseGeocode.notFound) {
+        endpoint.value = value;
+        input.addClass("is-invalid");
+      } else {
+        endpoint.value = endpoint.cachedReverseGeocode.value;
+      }
       input.val(endpoint.value);
       changeCallback();
       return;
@@ -86,6 +91,15 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch
     }
   };
 
+  endpoint.swapCachedReverseGeocodes = function (otherEndpoint) {
+    var g0 = endpoint.cachedReverseGeocode;
+    var g1 = otherEndpoint.cachedReverseGeocode;
+    delete endpoint.cachedReverseGeocode;
+    delete otherEndpoint.cachedReverseGeocode;
+    if (g0) otherEndpoint.cachedReverseGeocode = g0;
+    if (g1) endpoint.cachedReverseGeocode = g1;
+  };
+
   function getGeocode() {
     var viewbox = map.getBounds().toBBoxString(); // <sw lon>,<sw lat>,<ne lon>,<ne lat>
     var geocodeUrl = OSM.NOMINATIM_URL + "search?q=" + encodeURIComponent(endpoint.value) + "&format=json&viewbox=" + viewbox;
@@ -114,6 +128,7 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch
     endpoint.geocodeRequest = $.getJSON(reverseGeocodeUrl, function (json) {
       delete endpoint.geocodeRequest;
       if (!json || !json.display_name) {
+        endpoint.cachedReverseGeocode = { latlng: latlng, notFound: true };
         return;
       }
 
@@ -144,11 +159,5 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch
     input.val(latlng.lat + ", " + latlng.lng);
   }
 
-  function convertLatLngToZoomPrecision(latlng) {
-    var precision = OSM.zoomPrecision(map.getZoom());
-
-    return L.latLng(latlng.lat.toFixed(precision), latlng.lng.toFixed(precision));
-  }
-
   return endpoint;
 };