2 //= require_tree ./directions_engines
4 OSM.Directions = function (map) {
5 var awaitingGeocode; // true if the user has requested a route, but we're waiting on a geocode result
6 var awaitingRoute; // true if we've asked the engine for a route and are waiting to hear back
7 var dragging; // true if the user is dragging a start/end point
10 var popup = L.popup();
12 var polyline = L.polyline([], {
18 var highlight = L.polyline([], {
25 Endpoint($("input[name='route_from']"), <%= asset_path('marker-green.png').to_json %>),
26 Endpoint($("input[name='route_to']"), <%= asset_path('marker-red.png').to_json %>)
29 function Endpoint(input, iconUrl) {
32 endpoint.marker = L.marker([0, 0], {
37 popupAnchor: [1, -34],
38 shadowUrl: <%= asset_path('images/marker-shadow.png').to_json %>,
44 endpoint.marker.on('drag dragend', function (e) {
45 dragging = (e.type == 'drag');
46 if (dragging && !chosenEngine.draggable) return;
47 if (dragging && awaitingRoute) return;
48 endpoint.setLatLng(e.target.getLatLng());
49 r.requestRoute(!dragging, false);
52 input.on("change", function (e) {
53 endpoint.awaitingGeocode = true;
55 $.getJSON('<%= NOMINATIM_URL %>search?q=' + encodeURIComponent(e.target.value) + '&format=json', function (json) {
56 endpoint.awaitingGeocode = false;
58 if (json.length == 0) {
59 alert(I18n.t('javascripts.directions.errors.no_place'));
63 input.val(json[0].display_name);
65 endpoint.latlng = L.latLng(json[0]);
67 .setLatLng(endpoint.latlng)
70 if (awaitingGeocode) {
71 awaitingGeocode = false;
72 r.requestRoute(true, true);
77 endpoint.setLatLng = function (ll) {
78 input.val(Math.round(ll.lat * 10000) / 10000 + " " + Math.round(ll.lng * 10000) / 10000);
88 function formatDistance(m) {
90 return Math.round(m) + "m";
91 } else if (m < 10000) {
92 return (m / 1000.0).toFixed(1) + "km";
94 return Math.round(m / 1000) + "km";
98 function formatTime(s) {
99 var m = Math.round(s / 60);
100 var h = Math.floor(m / 60);
102 return h + ":" + (m < 10 ? '0' : '') + m;
105 var engines = OSM.Directions.engines;
107 engines.sort(function (a, b) {
108 return I18n.t(a.name) > I18n.t(b.name);
111 var select = $('select.routing_engines');
113 for (var i = 0; i < engines.length; i++) {
114 var engine = engines[i];
116 select.append("<option value='" + i + "'>" + I18n.t(engine.name) + "</option>");
118 if (engine.name == 'javascripts.directions.engines.osrm_car') {
119 chosenEngine = engine;
124 select.on("change", function (e) {
125 chosenEngine = engines[e.target.selectedIndex];
126 if (map.hasLayer(polyline)) { // and if a route is currently showing, must also refresh, else confusion
127 r.requestRoute(true, false);
131 $(".get_directions").on("click", function (e) {
134 $(".routing").show();
135 $(".search_form input[type='submit']").addClass("routing_submit");
136 $(".query_wrapper.routing [name=route_from]").focus();
138 $("#map").on('dragend dragover', function (e) {
142 $("#map").on('drop', function (e) {
144 var oe = e.originalEvent;
145 var id = oe.dataTransfer.getData('id');
146 var pt = L.DomEvent.getMousePosition(oe, map.getContainer()); // co-ordinates of the mouse pointer at present
147 pt.x += Number(oe.dataTransfer.getData('offsetX'));
148 pt.y += Number(oe.dataTransfer.getData('offsetY'));
149 var ll = map.containerPointToLatLng(pt);
150 endpoints[id === 'marker_from' ? 0 : 1].setLatLng(ll);
151 r.requestRoute(true, false);
154 $(".routing_marker").on('dragstart', function (e) {
155 e.originalEvent.dataTransfer.effectAllowed = 'move';
156 e.originalEvent.dataTransfer.setData('id', this.id);
157 var xo = e.originalEvent.clientX - $(e.target).offset().left;
158 var yo = e.originalEvent.clientY - $(e.target).offset().top;
159 e.originalEvent.dataTransfer.setData('offsetX', e.originalEvent.target.width / 2 - xo);
160 e.originalEvent.dataTransfer.setData('offsetY', e.originalEvent.target.height - yo);
164 $(".close_directions").on("click", function (e) {
167 $(".routing").hide();
168 $(".search_form input[type='submit']").removeClass("routing_submit");
169 $("#content").addClass("overlay-sidebar");
170 $(".query_wrapper.routing input").val("");
173 .removeLayer(polyline)
174 .removeLayer(endpoints[0].marker)
175 .removeLayer(endpoints[1].marker);
176 endpoints[0].latlng = endpoints[1].latlng = null;
177 $("#map").off('dragend drop dragover');
178 $(".routing_marker").off('dragstart');
179 $(".query_wrapper.search [name=query]").focus();
184 r.requestRoute = function (isFinal, updateZoom) {
185 if (endpoints[0].awaitingGeocode || endpoints[1].awaitingGeocode) {
186 awaitingGeocode = true;
190 var origin = endpoints[0].latlng,
191 destination = endpoints[1].latlng;
193 if (!origin || !destination) {
197 $(".query_wrapper.routing .spinner").show();
198 awaitingRoute = true;
201 map.fitBounds(L.latLngBounds(origin, destination).pad(0.05));
204 chosenEngine.getRoute(isFinal, [origin, destination], function (err, route) {
205 awaitingRoute = false;
207 $(".query_wrapper.routing .spinner").hide();
210 map.removeLayer(polyline);
213 alert(I18n.t('javascripts.directions.errors.no_route'));
220 .setLatLngs(route.line)
223 $("#content").removeClass("overlay-sidebar");
225 var html = ('<h2><a class="geolink" href="#" onclick="$(~.close_directions~).click();return false;">' +
226 '<span class="icon close"></span></a>' + I18n.t('javascripts.directions.directions') +
227 '</h2><p id="routing_summary">' +
228 I18n.t('javascripts.directions.distance') + ': ' + formatDistance(route.distance) + '. ' +
229 I18n.t('javascripts.directions.time') + ': ' + formatTime(route.time) + '.</p>' +
230 '<table id="turnbyturn" />').replace(/~/g, "'");
232 $('#sidebar_content')
237 route.steps.forEach(function (step) {
240 instruction = step[2],
248 } else if (dist < 200) {
249 dist = Math.round(dist / 10) * 10 + "m";
250 } else if (dist < 1500) {
251 dist = Math.round(dist / 100) * 100 + "m";
252 } else if (dist < 5000) {
253 dist = Math.round(dist / 100) / 10 + "km";
255 dist = Math.round(dist / 1000) + "km";
258 var row = $("<tr class='turn'/>");
259 row.append("<td class='direction i" + direction + "'> ");
260 row.append("<td class='instruction'>" + instruction);
261 row.append("<td class='distance'>" + dist);
263 row.on('click', function () {
266 .setContent("<p>" + instruction + "</p>")
270 row.hover(function () {
275 map.removeLayer(highlight);
278 $('#turnbyturn').append(row);
281 $('#sidebar_content').append('<p id="routing_credit">' +
282 I18n.t('javascripts.directions.instructions.courtesy', {link: chosenEngine.creditline}) +
290 OSM.Directions.engines = [];
292 OSM.Directions.addEngine = function (engine, supportsHTTPS) {
293 if (document.location.protocol == "http:" || supportsHTTPS) {
294 OSM.Directions.engines.push(engine);