Skip to content

Commit c380976

Browse files
committed
Get directions using fetch
1 parent 974e404 commit c380976

File tree

4 files changed

+46
-70
lines changed

4 files changed

+46
-70
lines changed

app/assets/javascripts/index/directions.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
//= require_tree ./directions
44

55
OSM.Directions = function (map) {
6-
var routeRequest = null; // jqXHR object of an ongoing route request or null
6+
let awaitingRoute = false; // true if a route request is in progress
7+
let controller = null; // the AbortController for the current route request
78
var chosenEngine;
89

910
var popup = L.popup({ autoPanPadding: [100, 100] });
@@ -23,7 +24,7 @@ OSM.Directions = function (map) {
2324
var endpointDragCallback = function (dragging) {
2425
if (!map.hasLayer(polyline)) return;
2526
if (dragging && !chosenEngine.draggable) return;
26-
if (dragging && routeRequest) return;
27+
if (dragging && awaitingRoute) return;
2728

2829
getRoute(false, !dragging);
2930
};
@@ -109,7 +110,7 @@ OSM.Directions = function (map) {
109110

110111
function getRoute(fitRoute, reportErrors) {
111112
// Cancel any route that is already in progress
112-
if (routeRequest) routeRequest.abort();
113+
if (controller) controller.abort();
113114

114115
const points = endpoints.map(p => p.latlng);
115116

@@ -126,20 +127,9 @@ OSM.Directions = function (map) {
126127
// again.
127128
$("#sidebar_content").html($(".directions_form .loader_copy").html());
128129
map.setSidebarOverlaid(false);
129-
130-
routeRequest = chosenEngine.getRoute(points, function (err, route) {
131-
routeRequest = null;
132-
133-
if (err) {
134-
map.removeLayer(polyline);
135-
136-
if (reportErrors) {
137-
$("#sidebar_content").html("<div class=\"alert alert-danger\">" + I18n.t("javascripts.directions.errors.no_route") + "</div>");
138-
}
139-
140-
return;
141-
}
142-
130+
controller = new AbortController();
131+
awaitingRoute = true;
132+
chosenEngine.getRoute(points, controller.signal).then(function (route) {
143133
polyline
144134
.setLatLngs(route.line)
145135
.addTo(map);
@@ -212,6 +202,13 @@ OSM.Directions = function (map) {
212202
map.setSidebarOverlaid(true);
213203
// TODO: collapse width of sidebar back to previous
214204
});
205+
}).catch(function () {
206+
map.removeLayer(polyline);
207+
if (reportErrors) {
208+
$("#sidebar_content").html("<div class=\"alert alert-danger\">" + I18n.t("javascripts.directions.errors.no_route") + "</div>");
209+
}
210+
}).finally(function () {
211+
awaitingRoute = false;
215212
});
216213

217214
function getDistText(dist) {

app/assets/javascripts/index/directions/fossgis_osrm.js

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -154,38 +154,31 @@
154154
creditline: "<a href=\"https://routing.openstreetmap.de/about.html\" target=\"_blank\">OSRM (FOSSGIS)</a>",
155155
draggable: true,
156156

157-
getRoute: function (points, callback) {
158-
const data = [
159-
{ name: "overview", value: "false" },
160-
{ name: "geometries", value: "polyline" },
161-
{ name: "steps", value: true }
162-
];
157+
getRoute: function (points, signal) {
158+
const query = new URLSearchParams({
159+
overview: "false",
160+
geometries: "polyline",
161+
steps: true
162+
});
163163

164164
if (cachedHints.length === points.length) {
165-
data.push({ name: "hints", value: cachedHints.join(";") });
165+
query.set("hints", cachedHints.join(";"));
166166
} else {
167167
// invalidate cache
168168
cachedHints = [];
169169
}
170170

171171
const req_path = "routed-" + vehicleType + "/route/v1/driving/" + points.map(p => p.lng + "," + p.lat).join(";");
172172

173-
return $.ajax({
174-
url: OSM.FOSSGIS_OSRM_URL + req_path,
175-
data,
176-
dataType: "json",
177-
success: function (response) {
173+
return fetch(OSM.FOSSGIS_OSRM_URL + req_path + "?" + query, { signal })
174+
.then(response => response.json())
175+
.then(response => {
178176
if (response.code !== "Ok") {
179-
return callback(true);
177+
throw new TypeError("Response " + response.code);
180178
}
181-
182179
cachedHints = response.waypoints.map(wp => wp.hint);
183-
callback(false, _processDirections(response.routes[0]));
184-
},
185-
error: function () {
186-
callback(true);
187-
}
188-
});
180+
return _processDirections(response.routes[0]);
181+
});
189182
}
190183
};
191184
}

app/assets/javascripts/index/directions/fossgis_valhalla.js

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@
8787
"<a href='https://gis-ops.com/global-open-valhalla-server-online/' target='_blank'>Valhalla (FOSSGIS)</a>",
8888
draggable: false,
8989

90-
getRoute: function (points, callback) {
91-
const data = {
90+
getRoute: function (points, signal) {
91+
const query = new URLSearchParams({
9292
json: JSON.stringify({
9393
locations: points.map(function (p) {
9494
return { lat: p.lat, lon: p.lng, radius: 5 };
@@ -99,22 +99,15 @@
9999
language: I18n.currentLocale()
100100
}
101101
})
102-
};
103-
return $.ajax({
104-
url: OSM.FOSSGIS_VALHALLA_URL,
105-
data,
106-
dataType: "json",
107-
success: function ({ trip }) {
108-
if (trip.status === 0) {
109-
callback(false, _processDirections(trip.legs));
110-
} else {
111-
callback(true);
112-
}
113-
},
114-
error: function () {
115-
callback(true);
116-
}
117102
});
103+
return fetch(OSM.FOSSGIS_VALHALLA_URL + "?" + query, { signal })
104+
.then(response => response.json())
105+
.then(({ trip }) => {
106+
if (trip.status !== 0) {
107+
throw new TypeError("Response status " + trip.status);
108+
}
109+
return _processDirections(trip.legs);
110+
});
118111
}
119112
};
120113
}

app/assets/javascripts/index/directions/graphhopper.js

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,33 +51,26 @@
5151
creditline: "<a href=\"https://www.graphhopper.com/\" target=\"_blank\">GraphHopper</a>",
5252
draggable: false,
5353

54-
getRoute: function (points, callback) {
54+
getRoute: function (points, signal) {
5555
// GraphHopper Directions API documentation
5656
// https://graphhopper.com/api/1/docs/routing/
57-
const data = {
57+
const query = new URLSearchParams({
5858
vehicle: vehicleType,
5959
locale: I18n.currentLocale(),
6060
key: "LijBPDQGfu7Iiq80w3HzwB4RUDJbMbhs6BU0dEnn",
6161
elevation: false,
6262
instructions: true,
6363
turn_costs: vehicleType === "car",
6464
point: points.map(p => p.lat + "," + p.lng)
65-
};
66-
return $.ajax({
67-
url: OSM.GRAPHHOPPER_URL,
68-
data,
69-
traditional: true,
70-
dataType: "json",
71-
success: function ({ paths }) {
65+
});
66+
return fetch(OSM.GRAPHHOPPER_URL + "?" + query, { signal })
67+
.then(response => response.json())
68+
.then(({ paths }) => {
7269
if (!paths || paths.length === 0) {
73-
return callback(true);
70+
throw new TypeError("Response empty");
7471
}
75-
callback(false, _processDirections(paths[0]));
76-
},
77-
error: function () {
78-
callback(true);
79-
}
80-
});
72+
return _processDirections(paths[0]);
73+
});
8174
}
8275
};
8376
}

0 commit comments

Comments
 (0)