1 GraphHopperEngine = function(vehicleName, vehicleParam, locale) {
2 this.vehicleName = vehicleName;
3 this.vehicleParam = vehicleParam;
4 //At this point the local system isn't correctly initialised yet, so we don't have accurate information about current locale
10 GraphHopperEngine.prototype.createConfig = function() {
13 name: "javascripts.directions.engines.graphhopper_"+this.vehicleName.toLowerCase(),
14 creditline: '<a href="http://graphhopper.com/" target="_blank">Graphhopper</a>',
17 getRoute: function(isFinal, points) {
18 var url = "http://graphhopper.com/routing/api/route?"
20 + "&locale=" + I18n.currentLocale();
21 for (var i = 0; i < points.length; i++) {
22 var pair = points[i].join(',');
23 url += "&point=" + pair;
26 url += "&instructions=true";
27 // GraphHopper supports json too
28 this.requestJSONP(url + "&type=jsonp&callback=");
30 gotRoute: function(router, data) {
31 if (!data.info.routeFound) {
35 var line = L.PolylineUtil.decode(data.route.coordinates);
36 router.setPolyline(line);
37 // Assemble instructions
39 var instr = data.route.instructions;
40 for (i = 0; i < instr.descriptions.length; i++) {
41 var indi = instr.indications[i];
42 var instrCode = (i == instr.descriptions.length - 1) ? 15 : this.GH_INSTR_MAP[indi];
43 var instrText = "<b>" + (i + 1) + ".</b> ";
44 instrText += instr.descriptions[i];
45 var latlng = instr.latLngs[i];
46 var distInMeter = instr.distances[i];
47 steps.push([{lat: latlng[0], lng: latlng[1]}, instrCode, instrText, distInMeter, []]); // TODO does graphhopper map instructions onto line indices?
49 router.setItinerary({ steps: steps, distance: data.route.distance, time: data.route['time']/1000 });
53 "-3": 6, // sharp left
55 "-1": 8, // slight left
64 OSM.RoutingEngines.add(false, new GraphHopperEngine("Bicycle", "vehicle=bike").createConfig());
65 OSM.RoutingEngines.add(false, new GraphHopperEngine("Foot", "vehicle=foot").createConfig());