googlemaps / google-maps-services-go

Go client library for Google Maps API Web Services
https://pkg.go.dev/googlemaps.github.io/maps
Apache License 2.0
748 stars 215 forks source link

Unable to get DurationInTraffic from DistanceMatrix #159

Closed ivan-californias closed 6 years ago

ivan-californias commented 6 years ago

Same problem as https://github.com/googlemaps/google-maps-services-go/issues/150

I'm trying to get duration_in_traffic field from Distance Matrix API, which is documented here: https://developers.google.com/maps/documentation/distance-matrix/intro#duration_in_traffic

According to documentation, I think I'm setting the required parameters for that field to be returned:

        r := &maps.DistanceMatrixRequest{
                Origins:       []string{fmt.Sprintf("%.7f,%.7f", lat1, lng1)},
                Destinations:  []string{fmt.Sprintf("%.7f,%.7f", lat2, lng2)},
                Mode:          maps.TravelModeDriving,
                Units:         maps.UnitsMetric,
                DepartureTime: "now", 
                TrafficModel:  maps.TrafficModelBestGuess,
        }
        distMatrix, err := c.DistanceMatrix(context.Background(), r)
        if err != nil {
                return nil, fmt.Errorf("Error getting response from google maps distance matrix api: %v", err)
        }
        bJson, _ := json.Marshal(distMatrix.Rows[0].Elements[0])
        log.Println(string(bJson))

Output:

{"status":"OK","distance":{"text":"2.0 km","value":2007},"duration":"value":298,"text":"4m58s"},"duration_in_traffic":null}

If I test with same api-key with curl, I do get data in that field:

$ curl 'https://maps.googleapis.com/maps/api/distancematrix/json?origins=19.3021022,-99.2298197&destinations=19.51018,-99.12585&mode=driving&units=metric&departure_time=now&traffic_model=best_guess&key=*********************'
{
   "destination_addresses" : [
      "Av Instituto Politécnico Nacional 3600, San Pedro Zacatenco, 07360 Ciudad de México, CDMX, Mexico"
   ],
   "origin_addresses" : [
      "Cto. Fuentes del Pedregal 555, Los Framboyanes, 14150 Ciudad de México, CDMX, Mexico"
   ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "30.5 km",
                  "value" : 30529
               },
               "duration" : {
                  "text" : "48 mins",
                  "value" : 2890
               },
               "duration_in_traffic" : {
                  "text" : "1 hour 6 mins",
                  "value" : 3942
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}
domesticmouse commented 6 years ago

I've added testing to make sure everything is round tripping through the parser correctly, and added to the distancematrix example app to enable testing your request. Everything looks correct in the library to me. What am I missing?

$ go run examples/distancematrix/cmdline/main.go -origins=19.3021022,-99.2298197 -destinations=19.51018,-99.12585 -mode=driving -units=metric -departure_time=now -traffic_model=best_guess -key=AIza...
&maps.DistanceMatrixResponse{
    OriginAddresses:      {"Cto. Fuentes del Pedregal 555, Los Framboyanes, 14150 Ciudad de México, CDMX, Mexico"},
    DestinationAddresses: {"Av Instituto Politécnico Nacional 3600, San Pedro Zacatenco, 07360 Ciudad de México, CDMX, Mexico"},
    Rows:                 {
        {
            Elements: {
                &maps.DistanceMatrixElement{
                    Status:            "OK",
                    Duration:          3487000000000,
                    DurationInTraffic: 3280000000000,
                    Distance:          maps.Distance{HumanReadable:"30.0 km", Meters:29987},
                },
            },
        },
    },
}
stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. Please comment here if it is still valid so that we can reprioritize. Thank you!