gpt4 book ai didi

javascript - OpenRouteService Directions api 用法

转载 作者:行者123 更新时间:2023-12-05 07:25:19 25 4
gpt4 key购买 nike

所以我正在尝试使用开放路线服务方向 API https://openrouteservice.org/dev/#/api-docs/directions/get但到目前为止,我找不到任何关于如何使用它的教程。

我很抱歉初学者的问题..这是我尝试使用的代码:

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();

问题是开始和结束坐标的值在链接内,我如何在其中放置一个变量,以便每次用户记录新坐标值时它都会在链接内更新?

最佳答案

您可以简单地使用 POST 方法而不是 Get 并将数据作为 JSON 数据在 Body 中发送。像下面的例子:

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);

关于javascript - OpenRouteService Directions api 用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54926373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com