X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/2cfc030bbac65d8cb687ee17e6fbf18ffa5ea2cc..41b086e217191a73b9c5afa297df6fb8c80bb2fb:/app/assets/javascripts/index/directions/fossgis_osrm.js diff --git a/app/assets/javascripts/index/directions/fossgis_osrm.js b/app/assets/javascripts/index/directions/fossgis_osrm.js index bb968f2da..4e8691a79 100644 --- a/app/assets/javascripts/index/directions/fossgis_osrm.js +++ b/app/assets/javascripts/index/directions/fossgis_osrm.js @@ -2,7 +2,7 @@ // Doesn't yet support hints (function () { - function FOSSGISOSRMEngine(id, vehicleType) { + function FOSSGISOSRMEngine(modeId, vehicleType) { let cachedHints = []; function _processDirections(route) { @@ -34,31 +34,31 @@ "arrive": "destination" }; const ICON_MAP = { - "continue": 0, - "merge right": 21, - "merge left": 20, - "off ramp right": 24, - "off ramp left": 25, - "on ramp right": 2, - "on ramp left": 6, - "fork right": 18, - "fork left": 19, - "end of road right": 22, - "end of road left": 23, - "turn straight": 0, - "turn slight right": 1, - "turn right": 2, - "turn sharp right": 3, - "turn uturn": 4, - "turn slight left": 5, - "turn left": 6, - "turn sharp left": 7, - "roundabout": 10, - "rotary": 10, - "exit roundabout": 10, - "exit rotary": 10, - "depart": 8, - "arrive": 14 + "continue": "straight", + "merge right": "merge-right", + "merge left": "merge-left", + "off ramp right": "exit-right", + "off ramp left": "exit-left", + "on ramp right": "right", + "on ramp left": "left", + "fork right": "fork-right", + "fork left": "fork-left", + "end of road right": "end-of-road-right", + "end of road left": "end-of-road-left", + "turn straight": "straight", + "turn slight right": "slight-right", + "turn right": "right", + "turn sharp right": "sharp-right", + "turn uturn": "u-turn", + "turn slight left": "slight-left", + "turn left": "left", + "turn sharp left": "sharp-left", + "roundabout": "roundabout", + "rotary": "roundabout", + "exit roundabout": "roundabout", + "exit rotary": "roundabout", + "depart": "start", + "arrive": "destination" }; function numToWord(num) { return ["first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth"][num - 1]; @@ -150,19 +150,20 @@ } return { - id: id, + mode: modeId, + provider: "fossgis_osrm", creditline: "OSRM (FOSSGIS)", draggable: true, - getRoute: function (points, callback) { - const data = [ - { name: "overview", value: "false" }, - { name: "geometries", value: "polyline" }, - { name: "steps", value: true } - ]; + getRoute: function (points, signal) { + const query = new URLSearchParams({ + overview: "false", + geometries: "polyline", + steps: true + }); if (cachedHints.length === points.length) { - data.push({ name: "hints", value: cachedHints.join(";") }); + query.set("hints", cachedHints.join(";")); } else { // invalidate cache cachedHints = []; @@ -170,27 +171,18 @@ const req_path = "routed-" + vehicleType + "/route/v1/driving/" + points.map(p => p.lng + "," + p.lat).join(";"); - return $.ajax({ - url: OSM.FOSSGIS_OSRM_URL + req_path, - data, - dataType: "json", - success: function (response) { - if (response.code !== "Ok") { - return callback(true); - } - + return fetch(OSM.FOSSGIS_OSRM_URL + req_path + "?" + query, { signal }) + .then(response => response.json()) + .then(response => { + if (response.code !== "Ok") throw new Error(); cachedHints = response.waypoints.map(wp => wp.hint); - callback(false, _processDirections(response.routes[0])); - }, - error: function () { - callback(true); - } - }); + return _processDirections(response.routes[0]); + }); } }; } - OSM.Directions.addEngine(new FOSSGISOSRMEngine("fossgis_osrm_car", "car"), true); - OSM.Directions.addEngine(new FOSSGISOSRMEngine("fossgis_osrm_bike", "bike"), true); - OSM.Directions.addEngine(new FOSSGISOSRMEngine("fossgis_osrm_foot", "foot"), true); + OSM.Directions.addEngine(new FOSSGISOSRMEngine("car", "car"), true); + OSM.Directions.addEngine(new FOSSGISOSRMEngine("bicycle", "bike"), true); + OSM.Directions.addEngine(new FOSSGISOSRMEngine("foot", "foot"), true); }());