- // OSRM car engine
- // *** this should all be shared from an OSRM library somewhere
- // *** need to clear hints at some point
-
- r.addEngine({
- name: 'Car (OSRM)',
- draggable: true,
- _hints: {},
- getRoute: function(final,points) {
- var url="http://router.project-osrm.org/viaroute?z=14&output=json";
- for (var i=0; i<points.length; i++) {
- var pair=points[i].join(',');
- url+="&loc="+pair;
- if (this._hints[pair]) url+= "&hint="+this._hints[pair];
- }
- if (final) url+="&instructions=true";
- this.requestJSONP(url);
- },
- gotRoute: function(data) {
- if (data.status==207) {
- alert("Couldn't find route between those two places");
- return false;
+ // Routing engine handling
+
+ // Add all engines
+ var list=OSM.RoutingEngines.list;
+ list.sort(function(a,b) { return I18n.t(a.name)>I18n.t(b.name); });
+ var select=r.jqSearch.find('select.routing_engines');
+ for (var i=0; i<list.length; i++) {
+ // Set up JSONP callback
+ with ({num: i}) {
+ list[num].requestJSONP=function(url) {
+ var script = document.createElement('script');
+ script.src = url+r.name+".gotRoute"+num;
+ document.body.appendChild(script);
+ };
+ list[num].requestCORS=function(url) {
+ $.ajax({ url: url, method: "GET", data: {}, dataType: 'json', success: r['gotRoute'+num] });
+ };
+ r['gotRoute'+num]=function(data) {
+ r.awaitingRoute=false;
+ $(".query_wrapper.routing .spinner").hide();
+ if (!list[num].gotRoute(r,data)) {
+ // No route found
+ if (r.polyline) {
+ map.removeLayer(r.polyline);
+ r.polyline=null;
+ }
+ if (!r.dragging) { alert(I18n.t('javascripts.directions.errors.no_route')); }
+ }
+ };
+ }
+ select.append("<option value='"+i+"'>"+I18n.t(list[i].name)+"</option>");
+ }
+ r.engines=list;
+ r.chosenEngine=list[0]; // default to first engine
+
+ // Choose an engine on dropdown change
+ r.selectEngine=function(e) {
+ r.chosenEngine=r.engines[e.target.selectedIndex];
+ if (r.polyline){ // and if a route is currently showing, must also refresh, else confusion
+ r.requestRoute(true, false);
+ }
+ };
+ // Choose an engine by name
+ r.chooseEngine=function(name) {
+ for (var i=0; i<r.engines.length; i++) {
+ if (r.engines[i].name==name) {
+ r.chosenEngine=r.engines[i];
+ r.jqSearch.find('select.routing_engines').val(i);