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 *** draggable start/end markers
10 *** translation (including all alerts and presentation)
12 *** URL history (or do we consciously not want to support that?)
14 *** add MapQuest engine
16 *** finish CloudMade engine
19 var TURN_INSTRUCTIONS=["",
21 "Slight right onto ", // 2
22 "Turn right onto ", // 3
23 "Sharp right onto ", // 4
25 "Sharp left onto ", // 6
26 "Turn left onto ", // 7
27 "Slight left onto ", // 8
30 "At roundabout take ", // 11
31 "Leave roundabout - ", // 12
32 "Stay on roundabout - ", // 13
33 "Start at end of ", // 14
34 "Reach destination", // 15
35 "Go against one-way on ", // 16
36 "End of one-way on "] // 17
38 var ROUTING_POLYLINE={
47 // common functions and constants, e.g. OSRM parser, can go here
50 OSM.Routing=function(map,name,jqSearch) {
52 r.map=map; // Leaflet map
53 r.name=name; // global variable name of this instance (needed for JSONP)
54 r.jqSearch=jqSearch; // JQuery object for search panel
64 r.geocode=function(id,event) { var _this=this;
65 var field=event.target;
66 var v=event.target.value;
67 // *** do something if v==''
68 var querystring = '<%= NOMINATIM_URL %>search?q=' + encodeURIComponent(v) + '&format=json';
69 // *** &accept-language=<%#= request.user_preferred_languages.join(',') %>
70 // *** prefer current viewport
71 $.getJSON(querystring, function(json) { _this._gotGeocode(json,field); });
74 r._gotGeocode=function(json,field) {
76 alert("Sorry, couldn't find that place."); // *** internationalise
80 field.value=json[0].display_name;
81 var lat=Number(json[0].lat), lon=Number(json[0].lon);
82 r[field.id]=[lat,lon];
88 r.requestRoute=function() {
89 if (r.route_from && r.route_to) {
90 r.chosenEngine.getRoute(true,[r.route_from,r.route_to]);
91 // then, when the route has been fetched, it'll call the engine's gotRoute function
95 // Take an array of Leaflet LatLngs and draw it as a polyline
96 r.setPolyline=function(line) {
97 if (r.polyline) map.removeLayer(r.polyline);
98 r.polyline=L.polyline(line, ROUTING_POLYLINE).addTo(r.map);
99 r.map.fitBounds(r.polyline.getBounds());
102 // Take an array of directions and write it out
103 // (we use OSRM's route_instructions format)
105 r.setItinerary=function(steps) {
107 $("#content").removeClass("overlay-sidebar");
108 $('#sidebar_content').empty();
109 var html='<h2><a class="geolink" href="#" onclick="$(~.close_directions~).click();return false;"><span class="icon close"></span></a>Directions</h2>'.replace(/~/g,"'");
110 html+="<table id='turnbyturn' />";
111 $('#sidebar_content').html(html);
114 for (var i=0; i<steps.length; i++) {
116 var instCodes=step[0].split('-');
117 // Assemble instruction text
118 var instText="<b>"+(i+1)+".</b> ";
119 instText+=TURN_INSTRUCTIONS[instCodes[0]];
120 if (instCodes[1]) { instText+="exit "+instCodes[1]+" "; }
121 if (instCodes[0]!=15) { instText+=step[1] ? "<b>"+step[1]+"</b>" : "(unnamed)"; }
124 if (dist<5) { dist=""; }
125 else if (dist<200) { dist=Math.round(dist/10)*10+"m"; }
126 else if (dist<1500) { dist=Math.round(dist/100)*100+"m"; }
127 else if (dist<5000) { dist=Math.round(dist/100)/10+"km"; }
128 else { dist=Math.round(dist/1000)+"km"; }
130 var row=$("<tr class='turn'/>");
131 row.append("<td class='direction i"+instCodes[0]+"'> ");
132 row.append("<td class='instruction'>"+instText);
133 row.append("<td class='distance'>"+dist);
134 with ({num: i, dist: step[3]}) {
135 row.on('click',function(e) {
136 r.clickTurn(num, r.polyline.getLatLngs()[dist]);
139 $('#turnbyturn').append(row);
143 r.clickTurn=function(num,latlng) {
144 L.popup().setLatLng(latlng).setContent("<p>"+(num+1)+"</p>").openOn(r.map);
148 // Close all routing UI
151 $("#content").addClass("overlay-sidebar");
152 if (r.polyline) map.removeLayer(r.polyline);
155 // Routing engine handling
158 var list=OSM.RoutingEngines.list;
159 list.sort(function(a,b) { return a.name>b.name; });
160 var select=r.jqSearch.find('select.routing_engines');
161 for (var i=0; i<list.length; i++) {
162 // Set up JSONP callback
164 list[num].requestJSONP=function(url) {
165 var script = document.createElement('script');
166 script.src = url+r.name+".gotRoute"+num;
167 document.body.appendChild(script);
169 r['gotRoute'+num]=function(data) { list[num].gotRoute(r,data); };
171 select.append("<option value='"+i+"'>"+list[i].name+"</option>");
174 r.chosenEngine=list[0]; // default to first engine
176 // Choose an engine on dropdown change
177 r.selectEngine=function(e) {
178 r.chosenEngine=r.engines[e.target.selectedIndex];
180 // Choose an engine by name
181 r.chooseEngine=function(name) {
182 for (var i=0; i<r.engines.length; i++) {
183 if (r.engines[i].name==name) {
184 r.chosenEngine=r.engines[i];
185 r.jqSearch.find('select.routing_engines').val(i);