gpt4 book ai didi

google-maps - 谷歌地图方向api考虑交通

转载 作者:行者123 更新时间:2023-12-01 02:00:21 30 4
gpt4 key购买 nike

我正在尝试编写一个简单的脚本,以便通过考虑交通情况在谷歌表中计算两个位置之间的旅行时间。

我正在使用 google Maps API 的类 DirectionFinder。

我已经设法计算出旅行所需的时间,但无论我输入什么出发时间,我的旅行时间都保持不变。知道我在做什么错吗?使用此类甚至可以考虑流量吗?我是否需要成为业务用户才能访问此内容?

这是我的代码:

function DrivingSeconds(origin, destination, Y, M, D, H, S) {
Utilities.sleep(1000);
var time= new Date(Y,M,D,H,S);
var directions = Maps.newDirectionFinder()
.setDepart(time)
.setOrigin(origin)
.setDestination(destination)
.setMode(Maps.DirectionFinder.Mode.DRIVING)
.getDirections();
return directions.routes[0].legs[0].duration.value;
}

感谢您提供的任何建议! :)

最佳答案

来自谷歌文档:

For requests where the travel mode is driving: You can specify the departure_time to receive a route and trip duration (response field: duration_in_traffic) that take traffic conditions into account. This option is only available if the request contains a valid API key, or a valid Google Maps APIs Premium Plan client ID and signature. The departure_time must be set to the current time or some time in the future. It cannot be in the past.



https://developers.google.com/maps/documentation/directions/intro#RequestParameters

所以是的,仅适用于高级用户。那么您的请求应如下所示:
var request = {
origin: origin,
destination: destination,
drivingOptions: {
departureTime: new Date(),
},
travelMode: google.maps.TravelMode[DRIVING]
};

var directionsService = new google.maps.DirectionsService();
directionsService.route(request, function(response,status) {
if(status == google.maps.DirectionsStatus.OK) {
console.log(response.routes[0].legs[0].duration_in_traffic);
}
});

关于google-maps - 谷歌地图方向api考虑交通,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37121436/

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