diff --git a/package.json b/package.json index 3b9fd89..ef9fef3 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/playground/index.ts b/playground/index.ts index 343d2d2..2b6189d 100644 --- a/playground/index.ts +++ b/playground/index.ts @@ -4,4 +4,15 @@ const ra = new RoutingApi("https://routing.anyways.eu/api/", "Vc32GLKD1wjxyiloWh ra.getProfiles(ps => { console.log(ps); -}); \ No newline at end of file +}); + +ra.getRoutes({ + locations: [ { + lng: 4.801197052001953, + lat: 51.267929925864756 + }, { + lng: 4.798192977905273, + lat: 51.25960528564359 + } ], + profiles: [ "bicycle.commute", "bicycle.shortest" ] +}, r => console.log(r)); \ No newline at end of file diff --git a/src/RoutingApi.ts b/src/RoutingApi.ts index 66b8f44..ebf3aca 100644 --- a/src/RoutingApi.ts +++ b/src/RoutingApi.ts @@ -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();