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