3

So I'm trying to use the open route service directions API https://openrouteservice.org/dev/#/api-docs/directions/get but so far I cannot find any tutorial on how to use it.

Im sorry for the beginers questions.. This is the code that I'm trying to use:

var request = new XMLHttpRequest();

request.open('GET', 'https://api.openrouteservice.org/directions?api_key=<API-KEY>&coordinates=24.942729,37.443186%7C24.943709,37.444405&profile=cycling-regular');

request.onreadystatechange = function () {
  if (this.readyState === 4) {
    console.log('Status:', this.status);
    console.log('Headers:', this.getAllResponseHeaders());
    console.log('Body:', this.responseText);
  }
};

request.send();

The problem is that the values of the start and end coordinates are inside the link, how i put a variable in there so that every time the user logs a new coordinate value it updates inside the link?

Humppakäräjät
  • 1,156
  • 2
  • 13
  • 17
  • 2
    `'https://api.openrouteservice.org/directions?api_key=5b3ce3597851110001cf62485edbe5d5a37b47e3b00a2cf7778fddbe&coordinates=' + coordinates + '&profile=cycling-regular'`? – Sergiu Paraschiv Feb 28 '19 at 14:12

1 Answers1

0

You can simply use POST method instead of Get and send data within your Body as JSON data. like example below:

let request = new XMLHttpRequest();

request.open('POST', "https://api.openrouteservice.org/v2/directions/cycling-regular/geojson");

request.setRequestHeader('Accept', 'application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8');
request.setRequestHeader('Content-Type', 'application/json');
request.setRequestHeader('Authorization', '5b3ce3597851110001cf62488303e1e1bcb64deb91105af7032e30d4');

request.onreadystatechange = function () {
  if (this.readyState === 4) {
    console.log('Status:', this.status);
    console.log('Headers:', this.getAllResponseHeaders());
    console.log('Body:', this.responseText);
  }
};

const body = '{"coordinates":[[-73.47882,45.56434],[-73.4853,45.5578]],"maneuvers":"false","options":{"avoid_features":["steps"]}}';

request.send(body);