Skip to content

Commit

Permalink
Added option to request multiple routes.
Browse files Browse the repository at this point in the history
  • Loading branch information
xivk committed Mar 14, 2021
1 parent cb4a1e3 commit 965c6b2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@anyways-open/routing-api",
"version": "0.0.5",
"version": "0.0.6",
"description": "An npm package to interact with the ANYWAYS routing-api.",
"main": "./dist/RoutingApi.js",
"scripts": {
Expand Down
13 changes: 12 additions & 1 deletion playground/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,15 @@ const ra = new RoutingApi("https://routing.anyways.eu/api/", "Vc32GLKD1wjxyiloWh

ra.getProfiles(ps => {
console.log(ps);
});
});

ra.getRoutes({
locations: [ {
lng: 4.801197052001953,
lat: 51.267929925864756
}, {
lng: 4.798192977905273,
lat: 51.25960528564359
} ],
profiles: [ "bicycle.commute", "bicycle.shortest" ]
}, r => console.log(r));
38 changes: 37 additions & 1 deletion src/RoutingApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,43 @@ export class RoutingApi {
callback(parse);
}
else {
console.log("getProfiles failed: " + xhr.status);
console.log("getRoute failed: " + xhr.status);
}
};
xhr.send();
}

getRoutes(options: { locations: { lng: number, lat: number }[], profiles: string[] }, callback: (routes: any) => void) : void {
const path = "v1/routes";

let loc = "";
for (const l in options.locations) {
const location = options.locations[l];
if (loc.length > 0) loc += "&";
loc = loc + `loc=${ location.lng },${ location.lat }`;
}
loc = loc + ``;

let prof = "";
for (const l in options.profiles) {
const profile = options.profiles[l];
if (prof.length > 0) prof += "&";
prof = prof + `profile=${ profile }`;
}

const url = `${ this.url }${ path }?apiKey=${ this.key }&${ prof }&${ loc }&format=multijson`;

const xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.onload = () => {
if (xhr.status === 200) {
const response = JSON.parse(xhr.responseText);

const parse = response;
callback(parse);
}
else {
console.log("getRoutes failed: " + xhr.status);
}
};
xhr.send();
Expand Down

0 comments on commit 965c6b2

Please sign in to comment.