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?)
15 *** add GraphHopper engine
18 var TURN_INSTRUCTIONS=["",
20 "Slight right onto ", // 2
21 "Turn right onto ", // 3
22 "Sharp right onto ", // 4
24 "Sharp left onto ", // 6
25 "Turn left onto ", // 7
26 "Slight left onto ", // 8
29 "At roundabout take ", // 11
30 "Leave roundabout - ", // 12
31 "Stay on roundabout - ", // 13
32 "Start at end of ", // 14
33 "Reach destination", // 15
34 "Go against one-way on ", // 16
35 "End of one-way on "] // 17
37 var ROUTING_POLYLINE={
46 // common functions and constants, e.g. OSRM parser, can go here
49 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
56 r.route_from=null; // null=unset, false=awaiting response, [lat,lon]=geocoded
58 r.awaitingGeocode=false;// true if the user has requested a route, but we're waiting on a geocode result
59 r.awaitingRoute=false; // true if we've asked the engine for a route and are waiting to hear back
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("Sorry, couldn't find that place."); // *** internationalise
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.updateMarkers(field.id);
109 if (r.awaitingGeocode) {
110 r.awaitingGeocode=false;
111 r.requestRoute(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[id.replace('marker','route')]=[ll.lat,ll.lng];
123 r.requestRoute(true);
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 r.updateMarkers=function(id) {
138 r.markerDragged=function(e) {
139 // marker has been dragged
140 if (e.type=='drag' && !r.chosenEngine.draggable) return;
141 if (e.type=='drag' && r.awaitingRoute) return;
142 var id=e.target.options.name;
143 var ll=e.target.getLatLng();
144 r[id.replace('marker','route')]=[ll.lat,ll.lng];
145 r.requestRoute(e.type=='dragend');
146 // update to/from field
151 r.requestRoute=function(final) {
152 if (r.route_from && r.route_to) {
153 r.awaitingRoute=true;
154 r.chosenEngine.getRoute(final,[r.route_from,r.route_to]);
155 // then, when the route has been fetched, it'll call the engine's gotRoute function
156 } else if (r.route_from==false || r.route_to==false) {
157 // we're waiting for a Nominatim response before we can request a route
158 r.awaitingGeocode=true;
162 // Take an array of Leaflet LatLngs and draw it as a polyline
163 r.setPolyline=function(line) {
164 if (r.polyline) map.removeLayer(r.polyline);
165 r.polyline=L.polyline(line, ROUTING_POLYLINE).addTo(r.map);
166 // r.map.fitBounds(r.polyline.getBounds());
167 // *** ^^^ we only want to do this for geocode-originated routes
170 // Take directions and write them out
171 // data = { steps: array of [latlng, sprite number, instruction text, distance in metres] }
172 // sprite numbers equate to OSRM's route_instructions turn values
174 r.setItinerary=function(data) {
176 $("#content").removeClass("overlay-sidebar");
177 $('#sidebar_content').empty();
178 var html='<h2><a class="geolink" href="#" onclick="$(~.close_directions~).click();return false;"><span class="icon close"></span></a>Directions</h2>'.replace(/~/g,"'");
179 html+="<table id='turnbyturn' />";
180 $('#sidebar_content').html(html);
183 for (var i=0; i<data.steps.length; i++) {
184 var step=data.steps[i];
187 if (dist<5) { dist=""; }
188 else if (dist<200) { dist=Math.round(dist/10)*10+"m"; }
189 else if (dist<1500) { dist=Math.round(dist/100)*100+"m"; }
190 else if (dist<5000) { dist=Math.round(dist/100)/10+"km"; }
191 else { dist=Math.round(dist/1000)+"km"; }
193 var row=$("<tr class='turn'/>");
194 row.append("<td class='direction i"+step[1]+"'> ");
195 row.append("<td class='instruction'>"+step[2]);
196 row.append("<td class='distance'>"+dist);
197 with ({ num: i, ll: step[0] }) {
198 row.on('click',function(e) { r.clickTurn(num, ll); });
200 $('#turnbyturn').append(row);
204 r.clickTurn=function(num,latlng) {
205 r.popup=L.popup().setLatLng(latlng).setContent("<p>"+(num+1)+"</p>").openOn(r.map);
208 // Close all routing UI
211 $("#content").addClass("overlay-sidebar");
212 var remove=[r.polyline,r.popup,r.marker_from,r.marker_to];
213 for (var i=0; i<remove.length; i++) {
214 if (remove[i]) map.removeLayer(remove[i]);
218 // Routing engine handling
221 var list=OSM.RoutingEngines.list;
222 list.sort(function(a,b) { return a.name>b.name; });
223 var select=r.jqSearch.find('select.routing_engines');
224 for (var i=0; i<list.length; i++) {
225 // Set up JSONP callback
227 list[num].requestJSONP=function(url) {
228 var script = document.createElement('script');
229 script.src = url+r.name+".gotRoute"+num;
230 document.body.appendChild(script);
232 r['gotRoute'+num]=function(data) { r.awaitingRoute=false; list[num].gotRoute(r,data); };
234 select.append("<option value='"+i+"'>"+list[i].name+"</option>");
237 r.chosenEngine=list[0]; // default to first engine
239 // Choose an engine on dropdown change
240 r.selectEngine=function(e) {
241 r.chosenEngine=r.engines[e.target.selectedIndex];
243 // Choose an engine by name
244 r.chooseEngine=function(name) {
245 for (var i=0; i<r.engines.length; i++) {
246 if (r.engines[i].name==name) {
247 r.chosenEngine=r.engines[i];
248 r.jqSearch.find('select.routing_engines').val(i);