From b4925d7b0dcb005cc501eeb90c72335e56468bf7 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Mon, 12 Aug 2024 05:19:22 +0300 Subject: [PATCH 1/3] Limit coordinate precision in drag listener --- app/assets/javascripts/index/directions-endpoint.js | 8 ++++++-- app/assets/javascripts/index/directions.js | 3 ++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/index/directions-endpoint.js b/app/assets/javascripts/index/directions-endpoint.js index 9ae7ce84ff..87bd112ca8 100644 --- a/app/assets/javascripts/index/directions-endpoint.js +++ b/app/assets/javascripts/index/directions-endpoint.js @@ -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); @@ -93,9 +93,13 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch } 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; diff --git a/app/assets/javascripts/index/directions.js b/app/assets/javascripts/index/directions.js index 7e8c18fb8c..391c1f9315 100644 --- a/app/assets/javascripts/index/directions.js +++ b/app/assets/javascripts/index/directions.js @@ -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(); From 4f3576e91bcad927f037cd0f5a962960b9b2a8b5 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Mon, 12 Aug 2024 05:39:58 +0300 Subject: [PATCH 2/3] Save lat/lon to input data attributes --- app/assets/javascripts/index/directions-endpoint.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/index/directions-endpoint.js b/app/assets/javascripts/index/directions-endpoint.js index 87bd112ca8..3ffb9b4987 100644 --- a/app/assets/javascripts/index/directions-endpoint.js +++ b/app/assets/javascripts/index/directions-endpoint.js @@ -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,12 +86,22 @@ 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); } From a088b136fb7066b8d318a431ecee98c803039eef Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Mon, 12 Aug 2024 05:41:30 +0300 Subject: [PATCH 3/3] Read lat/lon attributes when generating context menu directions urls --- app/assets/javascripts/index/contextmenu.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/index/contextmenu.js b/app/assets/javascripts/index/contextmenu.js index ffc0eadb77..ea284f29b9 100644 --- a/app/assets/javascripts/index/contextmenu.js +++ b/app/assets/javascripts/index/contextmenu.js @@ -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);