]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/routing.js.erb
ee76fb14f41444ef5ad0bfc07973955a296345ee
[rails.git] / app / assets / javascripts / routing.js.erb
1 /*
2         osm.org routing interface
3         
4         See also:
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
8
9         *** draggable start/end markers
10         *** click each part
11         *** translation (including all alerts and presentation)
12         *** export GPX
13 */
14
15 var TURN_INSTRUCTIONS=["",
16         "Continue on ",                         // 1
17         "Slight right onto ",           // 2
18         "Turn right onto ",                     // 3
19         "Sharp right onto ",            // 4
20         "U-turn along ",                        // 5
21         "Sharp left onto ",                     // 6
22         "Turn left onto ",                      // 7
23         "Slight left onto ",            // 8
24         "(via point) ",                         // 9
25         "Follow ",                                      // 10
26         "At roundabout take ",          // 11
27         "Leave roundabout - ",          // 12
28         "Stay on roundabout - ",        // 13
29         "Start at end of ",                     // 14
30         "Reach destination",            // 15
31         "Go against one-way on ",       // 16
32         "End of one-way on "]           // 17
33
34 var ROUTING_POLYLINE={
35         color: '#03f',
36         opacity: 0.3,
37         weight: 10
38 };
39
40
41 OSM.Routing=function(map,name,jqSearch) {
42         var r={};
43         r.map=map;                              // Leaflet map
44         r.name=name;                    // global variable name of this instance (needed for JSONP)
45         r.jqSearch=jqSearch;    // JQuery object for search panel
46
47         r.route_from=null;
48         r.route_to=null;
49         r.viaPoints=[];
50         r.polyline=null;
51
52         // Geocoding
53
54         r.geocode=function(id,event) { var _this=this;
55                 var field=event.target;
56                 var v=event.target.value;
57                 // *** do something if v==''
58                 var querystring = '<%= NOMINATIM_URL %>search?q=' + encodeURIComponent(v) + '&format=json';
59                 // *** &accept-language=<%#= request.user_preferred_languages.join(',') %>
60                 // *** prefer current viewport
61                 $.getJSON(querystring, function(json) { _this._gotGeocode(json,field); });
62         };
63         
64         r._gotGeocode=function(json,field) {
65                 if (json.length==0) {
66                         alert("Sorry, couldn't find that place.");      // *** internationalise
67                         r[field.id]=null;
68                         return;
69                 }
70                 field.value=json[0].display_name;
71                 var lat=Number(json[0].lat), lon=Number(json[0].lon);
72                 r[field.id]=[lat,lon];
73                 // ** update markers
74         };
75         
76         // Route-fetching UI
77
78         r.requestRoute=function() {
79                 if (r.route_from && r.route_to) {
80                         var chosen=jqSearch.find('select.routing_engines :selected').val();
81                         r.engines[chosen].getRoute(true,[r.route_from,r.route_to]);
82                         // then, when the route has been fetched, it'll call the engine's gotRoute function
83                 }
84         };
85
86         // Take an array of Leaflet LatLngs and draw it as a polyline
87         r.setPolyline=function(line) {
88                 if (r.polyline) map.removeLayer(r.polyline);
89                 r.polyline=L.polyline(line, ROUTING_POLYLINE).addTo(r.map);
90                 r.map.fitBounds(r.polyline.getBounds());
91         };
92
93         // Take an array of directions and write it out
94         // (we use OSRM's route_instructions format)
95         // *** translations?
96         r.setItinerary=function(steps) {
97                 $("#content").removeClass("overlay-sidebar");
98                 $('#sidebar_content').empty();
99                 var html='<h2><a class="geolink" href="#"><span class="icon close"></span></a>Directions</h2>';
100                 html+="<table>";
101                 for (var i=0; i<steps.length; i++) {
102                         var step=steps[i];
103                         var instCodes=step[0].split('-');
104                         // Assemble instruction text
105                         var instText="<b>"+(i+1)+".</b> ";
106                         instText+=TURN_INSTRUCTIONS[instCodes[0]];
107                         if (instCodes[1]) { instText+="exit "+instCodes[1]+" "; }
108                         if (instCodes[0]!=15) { instText+=step[1] ? "<b>"+step[1]+"</b>" : "(unnamed)"; }
109                         // Distance
110                         var dist=step[2];
111                         if (dist<5) { dist=""; }
112                         else if (dist<200) { dist=Math.round(dist/10)*10+"m"; }
113                         else if (dist<1500) { dist=Math.round(dist/100)*100+"m"; }
114                         else if (dist<5000) { dist=Math.round(dist/100)/10+"km"; }
115                         else { dist=Math.round(dist/1000)+"km"; }
116                         // Add to table
117                         html+="<tr>";
118                         html+="<td class='direction i"+instCodes[0]+"'> ";
119                         html+="<td class='instruction'>"+instText;
120                         html+="<td class='distance'>"+dist;
121                 }
122                 html+="</table>";
123         $('#sidebar_content').html(html);
124         };
125
126
127         // Add engines
128         
129         r.engines=[];
130         r.addEngine=function(engine) {
131                 // Save engine
132                 var i=r.engines.length;
133                 engine.subscript=i;
134                 r['engine'+i]=engine;
135                 r.engines.push(engine);
136
137                 // Add generic JSONP function
138                 engine.requestJSONP=function(url) {
139                         var script = document.createElement('script');
140                         script.src = url+"&jsonp="+r.name+".engine"+this.subscript+".gotRoute";
141                         // OSRM doesn't like non-alphanumeric, otherwise we could just do OSM.routing.engines["+engine.subscript+"].gotRoute
142                         document.body.appendChild(script); 
143                 };
144
145                 // Populate dropdown
146                 var select=jqSearch.find('select.routing_engines');
147                 select.append("<option value='"+i+"'>"+engine.name+"</option>");
148         };
149
150         // OSRM car engine
151         // *** this should all be shared from an OSRM library somewhere
152         // *** need to clear hints at some point
153
154         r.addEngine({
155                 name: 'Car (OSRM)',
156                 draggable: true,
157                 _hints: {},
158                 getRoute: function(final,points) {
159                         var url="http://router.project-osrm.org/viaroute?z=14&output=json";
160                         for (var i=0; i<points.length; i++) {
161                                 var pair=points[i].join(',');
162                                 url+="&loc="+pair;
163                                 if (this._hints[pair]) url+= "&hint="+this._hints[pair];
164                         }
165                         if (final) url+="&instructions=true";
166                         this.requestJSONP(url);
167                 },
168                 gotRoute: function(data) {
169                         if (data.status==207) {
170                                 alert("Couldn't find route between those two places");
171                                 return false;
172                         }
173                         // *** store hints
174                         var line=L.PolylineUtil.decode(data.route_geometry);
175                         for (i=0; i<line.length; i++) { line[i].lat/=10; line[i].lng/=10; }
176                         r.setPolyline(line);
177                         r.setItinerary(data.route_instructions);
178                 }
179         });
180
181         return r;
182 };