1 OSM.DirectionsRouteOutput = function (map) {
2 const popup = L.popup({ autoPanPadding: [100, 100] });
4 const polyline = L.polyline([], {
10 const highlight = L.polyline([], {
16 let downloadURL = null;
18 function translateDistanceUnits(m) {
19 return [m, "m", m / 1000, "km"];
22 function formatTotalDistance(minorValue, minorName, majorValue, majorName) {
23 const scope = "javascripts.directions.distance_in_units";
25 if (minorValue < 1000 || majorValue < 0.25) {
26 return OSM.i18n.t(minorName, { scope, distance: Math.round(minorValue) });
27 } else if (majorValue < 10) {
28 return OSM.i18n.t(majorName, { scope, distance: majorValue.toFixed(1) });
30 return OSM.i18n.t(majorName, { scope, distance: Math.round(majorValue) });
34 function formatStepDistance(minorValue, minorName, majorValue, majorName) {
35 const scope = "javascripts.directions.distance_in_units";
39 } else if (minorValue < 200) {
40 return OSM.i18n.t(minorName, { scope, distance: Math.round(minorValue / 10) * 10 });
41 } else if (minorValue < 1500 || majorValue < 0.25) {
42 return OSM.i18n.t(minorName, { scope, distance: Math.round(minorValue / 100) * 100 });
43 } else if (majorValue < 5) {
44 return OSM.i18n.t(majorName, { scope, distance: majorValue.toFixed(1) });
46 return OSM.i18n.t(majorName, { scope, distance: Math.round(majorValue) });
50 function formatHeight(minorValue, minorName) {
51 const scope = "javascripts.directions.distance_in_units";
53 return OSM.i18n.t(minorName, { scope, distance: Math.round(minorValue) });
56 function formatTime(s) {
57 let m = Math.round(s / 60);
58 const h = Math.floor(m / 60);
60 return h + ":" + (m < 10 ? "0" : "") + m;
63 function writeSummary(route) {
64 $("#directions_route_distance").val(formatTotalDistance(...translateDistanceUnits(route.distance)));
65 $("#directions_route_time").val(formatTime(route.time));
66 if (typeof route.ascend !== "undefined" && typeof route.descend !== "undefined") {
67 $("#directions_route_ascend_descend").prop("hidden", false);
68 $("#directions_route_ascend").val(formatHeight(...translateDistanceUnits(route.ascend)));
69 $("#directions_route_descend").val(formatHeight(...translateDistanceUnits(route.descend)));
71 $("#directions_route_ascend_descend").prop("hidden", true);
72 $("#directions_route_ascend").val("");
73 $("#directions_route_descend").val("");
77 function writeSteps(route) {
78 $("#directions_route_steps").empty();
80 for (const [i, [direction, instruction, dist, lineseg]] of route.steps.entries()) {
81 const row = $("<tr class='turn'/>").appendTo($("#directions_route_steps"));
84 row.append("<td class='border-0'><svg width='20' height='20' class='d-block'><use href='#routing-sprite-" + direction + "' /></svg></td>");
86 row.append("<td class='border-0'>");
88 row.append(`<td><b>${i + 1}.</b> ${instruction}`);
89 row.append("<td class='distance text-body-secondary text-end'>" + formatStepDistance(...translateDistanceUnits(dist)));
91 row.on("click", function () {
93 .setLatLng(lineseg[0])
94 .setContent(`<p><b>${i + 1}.</b> ${instruction}</p>`)
99 .on("mouseenter", function () {
104 .on("mouseleave", function () {
105 map.removeLayer(highlight);
110 const routeOutput = {};
112 routeOutput.write = function (route) {
114 .setLatLngs(route.line)
120 const blob = new Blob([JSON.stringify(polyline.toGeoJSON())], { type: "application/json" });
121 URL.revokeObjectURL(downloadURL);
122 downloadURL = URL.createObjectURL(blob);
123 $("#directions_route_download").prop("href", downloadURL);
125 $("#directions_route_credit")
127 .prop("href", route.creditlink);
130 routeOutput.fit = function () {
131 map.fitBounds(polyline.getBounds().pad(0.05));
134 routeOutput.isVisible = function () {
135 return map.hasLayer(polyline);
138 routeOutput.remove = function () {
141 .removeLayer(polyline);
143 $("#directions_route_steps").empty();
145 URL.revokeObjectURL(downloadURL);
146 $("#directions_route_download").prop("href", "");