1 //= require polyline_decoder
4 function GraphHopperEngine(modeId, vehicleType, profile) {
13 "4": "destination", // finish reached
14 "5": "destination", // via reached
18 "-98": "u-turn-left", // unknown direction u-turn
19 "-8": "u-turn-left", // left u-turn
20 "8": "u-turn-right" // right u-turn
23 function _processDirections(path) {
24 const line = OSM.decodePolyline(path.points, { precision: 5 });
26 const steps = path.instructions.map(instr => [
27 GH_INSTR_MAP[instr.sign],
30 line.slice(instr.interval[0], instr.interval[1] + 1)
33 steps.at(-1)[0] = "destination";
38 distance: path.distance,
39 time: path.time / 1000,
47 provider: "graphhopper",
50 getRoute: function (points, signal) {
51 // GraphHopper Directions API documentation https://docs.graphhopper.com
52 const query = new URLSearchParams({
54 locale: OSM.i18n.locale,
55 key: "7cb4eb19-e0f4-40a3-a5e0-f2c039366f32",
59 const demoQuery = new URLSearchParams({ profile });
61 for (const { lat, lng } of points) {
62 query.append("point", [lat, lng]);
63 demoQuery.append("point", [lat, lng]);
67 credit: "GraphHopper",
68 creditlink: "https://www.graphhopper.com/",
69 demolink: "https://graphhopper.com/maps/?" + demoQuery
72 return fetch(OSM.GRAPHHOPPER_URL + "?" + query, { signal })
73 .then(response => response.json())
74 .then(({ paths }) => {
75 if (!paths || paths.length === 0) throw new Error();
77 return { ..._processDirections(paths[0]), ...meta };
83 OSM.directionsEngines.add(new GraphHopperEngine("car", "car", "car"), true);
84 OSM.directionsEngines.add(new GraphHopperEngine("bicycle", "bike", "bike"), true);
85 OSM.directionsEngines.add(new GraphHopperEngine("foot", "foot", "foot"), true);