2 function GraphHopperEngine(modeId, vehicleType, profile) {
11 "4": "destination", // finish reached
12 "5": "destination", // via reached
16 "-98": "u-turn-left", // unknown direction u-turn
17 "-8": "u-turn-left", // left u-turn
18 "8": "u-turn-right" // right u-turn
21 function _processDirections(path) {
22 const line = L.PolylineUtil.decode(path.points);
24 const steps = path.instructions.map(instr => [
25 GH_INSTR_MAP[instr.sign],
28 line.slice(instr.interval[0], instr.interval[1] + 1)
31 steps.at(-1)[0] = "destination";
36 distance: path.distance,
37 time: path.time / 1000,
45 provider: "graphhopper",
48 getRoute: function (points, signal) {
49 // GraphHopper Directions API documentation https://docs.graphhopper.com
50 const query = new URLSearchParams({
52 locale: OSM.i18n.locale,
53 key: "7cb4eb19-e0f4-40a3-a5e0-f2c039366f32",
57 const demoQuery = new URLSearchParams({ profile });
59 for (const { lat, lng } of points) {
60 query.append("point", [lat, lng]);
61 demoQuery.append("point", [lat, lng]);
65 credit: "GraphHopper",
66 creditlink: "https://www.graphhopper.com/",
67 demolink: "https://graphhopper.com/maps/?" + demoQuery
70 return fetch(OSM.GRAPHHOPPER_URL + "?" + query, { signal })
71 .then(response => response.json())
72 .then(({ paths }) => {
73 if (!paths || paths.length === 0) throw new Error();
75 return { ..._processDirections(paths[0]), ...meta };
81 OSM.Directions.addEngine(new GraphHopperEngine("car", "car", "car"), true);
82 OSM.Directions.addEngine(new GraphHopperEngine("bicycle", "bike", "bike"), true);
83 OSM.Directions.addEngine(new GraphHopperEngine("foot", "foot", "foot"), true);