X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/a1a3b2c87e53c095d43bb9835590dc525122a062..87363858cb4e9cff9b5c29ac6e95425c5c3fab8b:/app/assets/javascripts/index/directions/graphhopper.js diff --git a/app/assets/javascripts/index/directions/graphhopper.js b/app/assets/javascripts/index/directions/graphhopper.js index 191475873..b3194d16c 100644 --- a/app/assets/javascripts/index/directions/graphhopper.js +++ b/app/assets/javascripts/index/directions/graphhopper.js @@ -1,5 +1,5 @@ (function () { - function GraphHopperEngine(id, vehicleType) { + function GraphHopperEngine(modeId, vehicleType) { const GH_INSTR_MAP = { "-3": 7, // sharp left "-2": 6, // left @@ -47,42 +47,34 @@ } return { - id: id, + mode: modeId, + provider: "graphhopper", creditline: "GraphHopper", draggable: false, - getRoute: function (points, callback) { + getRoute: function (points, signal) { // GraphHopper Directions API documentation // https://graphhopper.com/api/1/docs/routing/ - const data = { + const query = new URLSearchParams({ vehicle: vehicleType, locale: I18n.currentLocale(), key: "LijBPDQGfu7Iiq80w3HzwB4RUDJbMbhs6BU0dEnn", elevation: false, instructions: true, - turn_costs: vehicleType === "car", - point: points.map(p => p.lat + "," + p.lng) - }; - return $.ajax({ - url: OSM.GRAPHHOPPER_URL, - data, - traditional: true, - dataType: "json", - success: function ({ paths }) { - if (!paths || paths.length === 0) { - return callback(true); - } - callback(false, _processDirections(paths[0])); - }, - error: function () { - callback(true); - } + turn_costs: vehicleType === "car" }); + points.forEach(p => query.append("point", p.lat + "," + p.lng)); + return fetch(OSM.GRAPHHOPPER_URL + "?" + query, { signal }) + .then(response => response.json()) + .then(({ paths }) => { + if (!paths || paths.length === 0) throw new Error(); + return _processDirections(paths[0]); + }); } }; } - OSM.Directions.addEngine(new GraphHopperEngine("graphhopper_car", "car"), true); - OSM.Directions.addEngine(new GraphHopperEngine("graphhopper_bicycle", "bike"), true); - OSM.Directions.addEngine(new GraphHopperEngine("graphhopper_foot", "foot"), true); + OSM.Directions.addEngine(new GraphHopperEngine("car", "car"), true); + OSM.Directions.addEngine(new GraphHopperEngine("bicycle", "bike"), true); + OSM.Directions.addEngine(new GraphHopperEngine("foot", "foot"), true); }());