2 osm.org routing interface
5 https://github.com/apmon/openstreetmap-website/tree/routing2
6 https://github.com/apmon/openstreetmap-website/compare/routing2
7 https://github.com/apmon/openstreetmap-website/blob/9755c3ae0a8d0684d43760f91dc864ff42d8477a/app/views/routing/start.js.erb
9 *** translation (including all alerts and presentation)
11 *** URL history (or do we consciously not want to support that?)
15 var TURN_INSTRUCTIONS=[]
17 var ROUTING_POLYLINE={
26 // common functions and constants, e.g. OSRM parser, can go here
29 OSM.Routing=function(map,name,jqSearch) {
32 TURN_INSTRUCTIONS=["",
33 I18n.t('javascripts.directions.instructions.continue_on'), // 1
34 I18n.t('javascripts.directions.instructions.slight_right'), // 2
35 I18n.t('javascripts.directions.instructions.turn_right'), // 3
36 I18n.t('javascripts.directions.instructions.sharp_right'), // 4
37 I18n.t('javascripts.directions.instructions.uturn'), // 5
38 I18n.t('javascripts.directions.instructions.sharp_left'), // 6
39 I18n.t('javascripts.directions.instructions.turn_left'), // 7
40 I18n.t('javascripts.directions.instructions.slight_left'), // 8
41 I18n.t('javascripts.directions.instructions.via_point'), // 9
42 I18n.t('javascripts.directions.instructions.follow'), // 10
43 I18n.t('javascripts.directions.instructions.roundabout'), // 11
44 I18n.t('javascripts.directions.instructions.leave_roundabout'), // 12
45 I18n.t('javascripts.directions.instructions.stay_roundabout'), // 13
46 I18n.t('javascripts.directions.instructions.start'), // 14
47 I18n.t('javascripts.directions.instructions.destination'), // 15
48 I18n.t('javascripts.directions.instructions.against_oneway'), // 16
49 I18n.t('javascripts.directions.instructions.end_oneway')] // 17
51 r.map=map; // Leaflet map
52 r.name=name; // global variable name of this instance (needed for JSONP)
53 r.jqSearch=jqSearch; // JQuery object for search panel
55 r.route_from=null; // null=unset, false=awaiting response, [lat,lon]=geocoded
57 r.awaitingGeocode=false;// true if the user has requested a route, but we're waiting on a geocode result
58 r.awaitingRoute=false; // true if we've asked the engine for a route and are waiting to hear back
59 r.dragging=false; // true if the user is dragging a start/end point
60 r.viaPoints=[]; // not yet used
62 r.polyline=null; // Leaflet polyline object
63 r.popup=null; // Leaflet popup object
64 r.marker_from=null; // Leaflet from marker
65 r.marker_to=null; // Leaflet to marker
67 r.chosenEngine=null; // currently selected routing engine
69 var icon_from = L.icon({
70 iconUrl: <%= asset_path('marker-green.png').to_json %>,
73 popupAnchor: [1, -34],
74 shadowUrl: <%= asset_path('images/marker-shadow.png').to_json %>,
77 var icon_to = L.icon({
78 iconUrl: <%= asset_path('marker-red.png').to_json %>,
81 popupAnchor: [1, -34],
82 shadowUrl: <%= asset_path('images/marker-shadow.png').to_json %>,
88 r.geocode=function(id,event) { var _this=this;
89 var field=event.target;
90 var v=event.target.value;
91 // *** do something if v==''
92 var querystring = '<%= NOMINATIM_URL %>search?q=' + encodeURIComponent(v) + '&format=json';
93 // *** &accept-language=<%#= request.user_preferred_languages.join(',') %>
94 // *** prefer current viewport
96 $.getJSON(querystring, function(json) { _this._gotGeocode(json,field); });
99 r._gotGeocode=function(json,field) {
100 if (json.length==0) {
101 alert(I18n.t('javascripts.directions.errors.no_place'));
105 field.value=json[0].display_name;
106 var lat=Number(json[0].lat), lon=Number(json[0].lon);
107 r[field.id]=[lat,lon];
108 r.updateMarker(field.id);
109 if (r.awaitingGeocode) {
110 r.awaitingGeocode=false;
111 r.requestRoute(true, true);
115 // Drag and drop markers
117 r.handleDrop=function(e) {
118 var id=e.originalEvent.dataTransfer.getData('id');
119 var ll=r.map.mouseEventToLatLng(e.originalEvent);
120 // *** ^^^ this is slightly off - we need to work out the latLng of the tip
121 r.createMarker(ll,id);
122 r.setNumericInput(ll,id);
123 r.requestRoute(true, false);
124 // update to/from field
126 r.createMarker=function(latlng,id) {
127 if (r[id]) r.map.removeLayer(r[id]);
128 r[id]=L.marker(latlng, {
129 icon: id=='marker_from' ? icon_from : icon_to,
133 r[id].on('drag',r.markerDragged);
134 r[id].on('dragend',r.markerDragged);
136 // Update marker from geocoded route input
137 r.updateMarker=function(id) {
138 var m=id.replace('route','marker');
139 if (!r[m]) { r.createMarker(r[id],m); return; }
140 var ll=r[m].getLatLng();
141 if (ll.lat!=r[id][0] || ll.lng!=r[id][1]) {
142 r.createMarker(r[id],m);
145 // Marker has been dragged
146 r.markerDragged=function(e) {
147 r.dragging=(e.type=='drag'); // true for drag, false for dragend
148 if (r.dragging && !r.chosenEngine.draggable) return;
149 if (r.dragging && r.awaitingRoute) return;
150 r.setNumericInput(e.target.getLatLng(), e.target.options.name);
151 r.requestRoute(!r.dragging, false);
153 // Set a route input field to a numeric value
154 r.setNumericInput=function(ll,id) {
155 var routeid=id.replace('marker','route');
156 r[routeid]=[ll.lat,ll.lng];
157 $("[name="+routeid+"]:visible").val(Math.round(ll.lat*10000)/10000+" "+Math.round(ll.lng*10000)/10000);
162 r.requestRoute=function(isFinal, updateZoom) {
163 if (r.route_from && r.route_to) {
164 $(".query_wrapper.routing .spinner").show();
165 r.awaitingRoute=true;
166 r.chosenEngine.getRoute(isFinal,[r.route_from,r.route_to]);
168 r.map.fitBounds(L.latLngBounds([r.route_from, r.route_to]).pad(0.05));
170 // then, when the route has been fetched, it'll call the engine's gotRoute function
171 } else if (r.route_from==false || r.route_to==false) {
172 // we're waiting for a Nominatim response before we can request a route
173 r.awaitingGeocode=true;
177 // Take an array of Leaflet LatLngs and draw it as a polyline
178 r.setPolyline=function(line) {
179 if (r.polyline) map.removeLayer(r.polyline);
180 r.polyline=L.polyline(line, ROUTING_POLYLINE).addTo(r.map);
181 // r.map.fitBounds(r.polyline.getBounds());
182 // *** ^^^ we only want to do this for geocode-originated routes
185 // Take directions and write them out
186 // data = { steps: array of [latlng, sprite number, instruction text, distance in metres] }
187 // sprite numbers equate to OSRM's route_instructions turn values
188 r.setItinerary=function(data) {
190 $("#content").removeClass("overlay-sidebar");
191 $('#sidebar_content').empty();
192 var html=('<h2><a class="geolink" href="#" onclick="$(~.close_directions~).click();return false;">' +
193 '<span class="icon close"></span></a>' + I18n.t('javascripts.directions.directions') +
194 '</h2><table id="turnbyturn" />').replace(/~/g,"'");
195 $('#sidebar_content').html(html);
198 for (var i=0; i<data.steps.length; i++) {
199 var step=data.steps[i];
202 if (dist<5) { dist=""; }
203 else if (dist<200) { dist=Math.round(dist/10)*10+"m"; }
204 else if (dist<1500) { dist=Math.round(dist/100)*100+"m"; }
205 else if (dist<5000) { dist=Math.round(dist/100)/10+"km"; }
206 else { dist=Math.round(dist/1000)+"km"; }
208 var row=$("<tr class='turn'/>");
209 row.append("<td class='direction i"+step[1]+"'> ");
210 row.append("<td class='instruction'>"+step[2]);
211 row.append("<td class='distance'>"+dist);
212 with ({ num: i, ll: step[0] }) {
213 row.on('click',function(e) { r.clickTurn(num, ll); });
215 $('#turnbyturn').append(row);
218 $('#sidebar_content').append('<p id="routing_credit">' + r.chosenEngine.creditline + '</p>');
221 r.clickTurn=function(num,latlng) {
222 r.popup=L.popup().setLatLng(latlng).setContent("<p>"+(num+1)+"</p>").openOn(r.map);
225 // Close all routing UI
228 $("#content").addClass("overlay-sidebar");
229 r.route_from=r.route_to=null;
230 $(".query_wrapper.routing input").val("");
231 var remove=['polyline','popup','marker_from','marker_to'];
232 for (var i=0; i<remove.length; i++) {
233 if (r[remove[i]]) { map.removeLayer(r[remove[i]]); r[remove[i]]=null; }
237 // Routing engine handling
240 var list=OSM.RoutingEngines.list;
241 list.sort(function(a,b) { return I18n.t(a.name)>I18n.t(b.name); });
242 var select=r.jqSearch.find('select.routing_engines');
243 for (var i=0; i<list.length; i++) {
244 // Set up JSONP callback
246 list[num].requestJSONP=function(url) {
247 var script = document.createElement('script');
248 script.src = url+r.name+".gotRoute"+num;
249 document.body.appendChild(script);
251 r['gotRoute'+num]=function(data) {
252 r.awaitingRoute=false;
253 $(".query_wrapper.routing .spinner").hide();
254 if (!list[num].gotRoute(r,data)) {
257 map.removeLayer(r.polyline);
260 if (!r.dragging) { alert(I18n.t('javascripts.directions.errors.no_route')); }
264 select.append("<option value='"+i+"'>"+I18n.t(list[i].name)+"</option>");
267 r.chosenEngine=list[0]; // default to first engine
269 // Choose an engine on dropdown change
270 r.selectEngine=function(e) {
271 r.chosenEngine=r.engines[e.target.selectedIndex];
272 if (r.polyline){ // and if a route is currently showing, must also refresh, else confusion
273 r.requestRoute(true, false);
276 // Choose an engine by name
277 r.chooseEngine=function(name) {
278 for (var i=0; i<r.engines.length; i++) {
279 if (r.engines[i].name==name) {
280 r.chosenEngine=r.engines[i];
281 r.jqSearch.find('select.routing_engines').val(i);