我用Golang编写了一些代码,使用Google Maps API从一个地方到另一个地方获取方向。根据Godoc, DepartureTime
和ArrivalTime
参数均为可选参数。但是,如果我在没有指定出发时间的情况下尝试提出任何请求,代码就会失败,给我以下错误信息:INVALID_REQUEST - Invalid request. Missing the 'departure_time' parameter
。以下是相关代码:
client, err := maps.NewClient(maps.WithAPIKey(apiKey), maps.WithRateLimit(2))
check(err, "new maps client")
r := &maps.DirectionsRequest{
Origin: origin,
Destination: destination,
ArrivalTime: arrivalTime,
Alternatives: alternatives,
Mode: maps.TravelModeDriving,
}
lookupTrafficModel(trafficModel, r)
if avoid != "" {
lookupAvoidPoints(avoid, r)
}
//THIS LINE IS WHERE THE ERROR IS THROWN
routes, waypoints, err := client.Directions(context.Background(), r)
check(err, "getting directions")
fmt.Println(waypoints)
fmt.Println(routes)
注意,如果我注释掉ArrivalTime
参数,而使用DepartureTime: "now",
(根据Godoc),这段代码将按预期工作。
这似乎是Google Maps API的限制,而不是Go客户端。对于那些在将来看到这个问题的人,尝试不同的参数组合(不考虑不同的),直到它有效。