]> git.openstreetmap.org Git - rails.git/commitdiff
Merge remote-tracking branch 'upstream/pull/5099'
authorTom Hughes <tom@compton.nu>
Tue, 20 Aug 2024 18:12:13 +0000 (19:12 +0100)
committerTom Hughes <tom@compton.nu>
Tue, 20 Aug 2024 18:12:13 +0000 (19:12 +0100)
app/assets/javascripts/index/contextmenu.js
app/assets/javascripts/index/directions-endpoint.js
app/assets/javascripts/index/directions.js

index ffc0eadb771d24810ba21c2e1344d20484726f27..ea284f29b97d87f0ee2508697b6afb1228c8a5b2 100644 (file)
@@ -11,7 +11,7 @@ OSM.initializeContextMenu = function (map) {
 
       OSM.router.route("/directions?" + Qs.stringify({
         from: lat + "," + lng,
-        to: $("#route_to").val()
+        to: getDirectionsEndpointCoordinatesFromInput($("#route_to"))
       }));
     }
   });
@@ -25,7 +25,7 @@ OSM.initializeContextMenu = function (map) {
           lng = latlng.lng.toFixed(precision);
 
       OSM.router.route("/directions?" + Qs.stringify({
-        from: $("#route_from").val(),
+        from: getDirectionsEndpointCoordinatesFromInput($("#route_from")),
         to: lat + "," + lng
       }));
     }
@@ -79,6 +79,14 @@ OSM.initializeContextMenu = function (map) {
     else map.contextmenu.enable();
   });
 
+  function getDirectionsEndpointCoordinatesFromInput(input) {
+    if (input.attr("data-lat") && input.attr("data-lon")) {
+      return input.attr("data-lat") + "," + input.attr("data-lon");
+    } else {
+      return $(input).val();
+    }
+  }
+
   var updateMenu = function updateMenu() {
     map.contextmenu.setDisabled(2, map.getZoom() < 12);
     map.contextmenu.setDisabled(4, map.getZoom() < 14);
index 9ae7ce84fff948bbf4a7303bfc07f794dc617e77..3ffb9b498723f06363cfe1f94c246e39ec864ca9 100644 (file)
@@ -31,7 +31,7 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch
   };
 
   function markerDragListener(e) {
-    var latlng = e.target.getLatLng();
+    var latlng = convertLatLngToZoomPrecision(e.target.getLatLng());
 
     setLatLng(latlng);
     setInputValueFromLatLng(latlng);
@@ -51,7 +51,7 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch
 
   endpoint.setValue = function (value, latlng) {
     endpoint.value = value;
-    delete endpoint.latlng;
+    removeLatLng();
     input.removeClass("is-invalid");
     input.val(value);
 
@@ -86,16 +86,30 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch
   }
 
   function setLatLng(ll) {
+    input
+      .attr("data-lat", ll.lat)
+      .attr("data-lon", ll.lng);
     endpoint.latlng = ll;
     endpoint.marker
       .setLatLng(ll)
       .addTo(map);
   }
 
+  function removeLatLng() {
+    input
+      .removeAttr("data-lat")
+      .removeAttr("data-lon");
+    delete endpoint.latlng;
+  }
+
   function setInputValueFromLatLng(latlng) {
+    input.val(latlng.lat + ", " + latlng.lng);
+  }
+
+  function convertLatLngToZoomPrecision(latlng) {
     var precision = OSM.zoomPrecision(map.getZoom());
 
-    input.val(latlng.lat.toFixed(precision) + ", " + latlng.lng.toFixed(precision));
+    return L.latLng(latlng.lat.toFixed(precision), latlng.lng.toFixed(precision));
   }
 
   return endpoint;
index 7e8c18fb8cdaafd6d0d0ace0542a8a780d5eadb3..391c1f9315c7fe945d822d49380f9578729e79c0 100644 (file)
@@ -287,7 +287,8 @@ OSM.Directions = function (map) {
       var ll = map.containerPointToLatLng(pt);
       var precision = OSM.zoomPrecision(map.getZoom());
       var value = ll.lat.toFixed(precision) + ", " + ll.lng.toFixed(precision);
-      endpoints[type === "from" ? 0 : 1].setValue(value, ll);
+      var llWithPrecision = L.latLng(ll.lat.toFixed(precision), ll.lng.toFixed(precision));
+      endpoints[type === "from" ? 0 : 1].setValue(value, llWithPrecision);
     });
 
     endpoints[0].enable();