I am trying to understand DistanceMatrix API. When I do this on browser:
I get the duration_in_traffic.
{
"destination_addresses" : [
"17 Orchard Rd, Bagumbayan, Quezon City, 1109 Metro Manila, Philippines"
],
"origin_addresses" : [ "74 C. Benitez St, Quezon City, Metro Manila, Philippines" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "8.5 km",
"value" : 8470
},
"duration" : {
"text" : "23 mins",
"value" : 1406
},
"duration_in_traffic" : {
"text" : "35 mins",
"value" : 2112
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
But when I have it in golang using maps API
I do not get the duration_in_traffic and returns only null
r := &maps.DistanceMatrixRequest{
Language: *language,
DepartureTime: "now",
ArrivalTime: *arrivalTime,
}
Below is result
{
"origin_addresses": [
"74 C. Benitez St, Quezon City, Metro Manila, Philippines"
],
"destination_addresses": [
"17 Orchard Rd, Bagumbayan, Quezon City, 1109 Metro Manila, Philippines"
],
"rows": [
{
"elements": [
{
"status": "OK",
"distance": {
"text": "8.5 km",
"value": 8470
},
"duration": {
"value": 1406,
"text": "23m26s"
},
"duration_in_traffic": null
}
]
}
]
}
Anything I am doing wrong?
EDIT:
r := &maps.DistanceMatrixRequest{
Origins: strings.Split(origins, "|"),
Destinations: strings.Split(destinations, "|"),
Language: "en",
DepartureTime: "now",
Mode: maps.TravelModeDriving,
Units: maps.UnitsMetric,
}
resp, err := client.DistanceMatrix(context.Background(), r)
if err != nil {
return c.JSON(http.StatusBadRequest, err)
}
return c.JSON(http.StatusOK, resp)
Was running into the same issue as you, and the edit to your post solved my issue.
To clarify the solution for anyone who may come along later- the issue was that I wasn't specifying the DepartureTime
in my request.
If you run the below code with DepartureTime
commented-out, it prints:
go run traffic.go
Duration in minutes: 0.000000
But with the DepartureTime: "now"
specified in the request, it gives:
go run traffic.go
Duration in minutes: 135.966667
Here's what the full working example looks like.
package main
import (
"fmt"
"log"
"golang.org/x/net/context"
"googlemaps.github.io/maps"
)
func main() {
c, err := maps.NewClient(maps.WithAPIKey("my-api-key-here"))
if err != nil {
log.Fatalf("fatal error: %s", err)
}
r := &maps.DistanceMatrixRequest{
Origins: []string{"Newark, DE"},
Destinations: []string{"Charles Town, WV"},
Units: maps.UnitsImperial,
Language: "en",
// Must specify DepartureTime in order to get DurationInTraffic in response
DepartureTime: "now",
}
route, err := c.DistanceMatrix(context.Background(), r)
if err != nil {
log.Fatalf("fatal error: %s", err)
}
fmt.Printf("Duration in minutes: %f\n", route.Rows[0].Elements[0].DurationInTraffic.Minutes())
}
Another potential gotcha is that Google does not return duration_in_traffic if it has no traffic data.
The duration in traffic is returned only if all of the following are true:
The request includes a departure_time parameter. The request includes a valid API key, or a valid Google Maps Platform Premium Plan client ID and signature. Traffic conditions are available for the requested route. The mode parameter is set to driving.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.