Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/pull/5099'
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhughes committed Aug 20, 2024
2 parents 9bee661 + a088b13 commit 20f8fad
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
12 changes: 10 additions & 2 deletions app/assets/javascripts/index/contextmenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}));
}
});
Expand All @@ -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
}));
}
Expand Down Expand Up @@ -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);
Expand Down
20 changes: 17 additions & 3 deletions app/assets/javascripts/index/directions-endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion app/assets/javascripts/index/directions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 20f8fad

Please sign in to comment.