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) {
19 // https://github.com/graphhopper/graphhopper/blob/master/docs/web/api-doc.md
20 var url = "http://graphhopper.com/api/1/route?"
22 + "&locale=" + I18n.currentLocale()
23 + "&key=LijBPDQGfu7Iiq80w3HzwB4RUDJbMbhs6BU0dEnn";
25 for (var i = 0; i < points.length; i++) {
26 var pair = points[i].join(',');
27 url += "&point=" + pair;
30 url += "&instructions=true";
31 // GraphHopper supports json too
32 this.requestJSONP(url + "&type=jsonp&callback=");
34 gotRoute: function(router, data) {
35 if (!data.paths || data.paths.length == 0)
39 var path = data.paths[0];
40 var line = L.PolylineUtil.decode(path.points);
41 router.setPolyline(line);
42 // Assemble instructions
44 var len = path.instructions.length;
45 for (i = 0; i < len; i++) {
46 var instr = path.instructions[i];
47 var instrCode = (i === len - 1) ? 15 : this.GH_INSTR_MAP[instr.sign];
48 var instrText = "<b>" + (i + 1) + ".</b> ";
49 instrText += instr.text;
50 var latLng = line[instr.interval[0]];
51 var distInMeter = instr.distance;
52 steps.push([{lat: latLng.lat, lng: latLng.lng}, instrCode, instrText, distInMeter, []]); // TODO does graphhopper map instructions onto line indices?
54 router.setItinerary({ steps: steps, distance: path.distance, time: path.time / 1000 });
58 "-3": 6, // sharp left
60 "-1": 8, // slight left
65 4: -1, // finish reached
71 OSM.RoutingEngines.add(false, new GraphHopperEngine("Bicycle", "vehicle=bike").createConfig());
72 OSM.RoutingEngines.add(false, new GraphHopperEngine("Foot", "vehicle=foot").createConfig());