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 distanceUnits = "km";
17 let downloadURL = null;
19 function translateDistanceUnits(m) {
20 if (distanceUnits === "km") {
21 return [m, "m", m / 1000, "km"];
23 return [m / 0.3048, "ft", m / 1609.344, "mi"];
27 function formatTotalDistance(minorValue, minorName, majorValue, majorName) {
28 const scope = "javascripts.directions.distance_in_units";
30 if (minorValue < 1000 || majorValue < 0.25) {
31 return OSM.i18n.t(minorName, { scope, distance: Math.round(minorValue) });
32 } else if (majorValue < 10) {
33 return OSM.i18n.t(majorName, { scope, distance: majorValue.toFixed(1) });
35 return OSM.i18n.t(majorName, { scope, distance: Math.round(majorValue) });
39 function formatStepDistance(minorValue, minorName, majorValue, majorName) {
40 const scope = "javascripts.directions.distance_in_units";
44 } else if (minorValue < 200) {
45 return OSM.i18n.t(minorName, { scope, distance: Math.round(minorValue / 10) * 10 });
46 } else if (minorValue < 1500 || majorValue < 0.25) {
47 return OSM.i18n.t(minorName, { scope, distance: Math.round(minorValue / 100) * 100 });
48 } else if (majorValue < 5) {
49 return OSM.i18n.t(majorName, { scope, distance: majorValue.toFixed(1) });
51 return OSM.i18n.t(majorName, { scope, distance: Math.round(majorValue) });
55 function formatHeight(minorValue, minorName) {
56 const scope = "javascripts.directions.distance_in_units";
58 return OSM.i18n.t(minorName, { scope, distance: Math.round(minorValue) });
61 function formatTime(s) {
62 let m = Math.round(s / 60);
63 const h = Math.floor(m / 60);
65 return h + ":" + (m < 10 ? "0" : "") + m;
68 function writeSummary(route) {
69 $("#directions_route_distance").val(formatTotalDistance(...translateDistanceUnits(route.distance)));
70 $("#directions_route_time").val(formatTime(route.time));
71 if (typeof route.ascend !== "undefined" && typeof route.descend !== "undefined") {
72 $("#directions_route_ascend_descend").prop("hidden", false);
73 $("#directions_route_ascend").val(formatHeight(...translateDistanceUnits(route.ascend)));
74 $("#directions_route_descend").val(formatHeight(...translateDistanceUnits(route.descend)));
76 $("#directions_route_ascend_descend").prop("hidden", true);
77 $("#directions_route_ascend").val("");
78 $("#directions_route_descend").val("");
82 function writeSteps(route) {
83 $("#directions_route_steps").empty();
85 for (const [i, [direction, instruction, dist, lineseg]] of route.steps.entries()) {
86 const row = $("<tr class='turn'/>").appendTo($("#directions_route_steps"));
89 row.append("<td class='border-0'><svg width='20' height='20' class='d-block'><use href='#routing-sprite-" + direction + "' /></svg></td>");
91 row.append("<td class='border-0'>");
93 row.append(`<td><b>${i + 1}.</b> ${instruction}`);
94 row.append("<td class='distance text-body-secondary text-end'>" + formatStepDistance(...translateDistanceUnits(dist)));
96 row.on("click", function () {
98 .setLatLng(lineseg[0])
99 .setContent(`<p><b>${i + 1}.</b> ${instruction}</p>`)
104 .on("mouseenter", function () {
109 .on("mouseleave", function () {
110 map.removeLayer(highlight);
115 const routeOutput = {};
117 routeOutput.write = function (route) {
119 .setLatLngs(route.line)
125 $("#directions_distance_units_km").off().on("change", () => {
126 distanceUnits = "km";
130 $("#directions_distance_units_mi").off().on("change", () => {
131 distanceUnits = "mi";
136 const blob = new Blob([JSON.stringify(polyline.toGeoJSON())], { type: "application/json" });
137 URL.revokeObjectURL(downloadURL);
138 downloadURL = URL.createObjectURL(blob);
139 $("#directions_route_download").prop("href", downloadURL);
141 $("#directions_route_credit")
143 .prop("href", route.creditlink);
146 routeOutput.fit = function () {
147 map.fitBounds(polyline.getBounds().pad(0.05));
150 routeOutput.isVisible = function () {
151 return map.hasLayer(polyline);
154 routeOutput.remove = function () {
157 .removeLayer(polyline);
159 $("#directions_distance_units_km").off();
160 $("#directions_distance_units_mi").off();
162 $("#directions_route_steps").empty();
164 URL.revokeObjectURL(downloadURL);
165 $("#directions_route_download").prop("href", "");