简体   繁体   中英

Google Maps Distance Matrix API gives impossibly short duration_in_traffic results and also the results are different than Google Maps

I am making a request to Google Maps Distance Matrix API to get duration_in_traffic data for today's afternoon between two points -Besiktas and Bosphorus Bridge- in Istanbul and I've set the departure time to be 17:00:00 at Mar 06 2018. It returns me that it takes 5 minutes and this is literally impossible, it should have been at least 20 mins. Also the results in Google Maps are different.

Here's URL I used: https://maps.googleapis.com/maps/api/distancematrix/json?&departure_time=1520301600000&traffic_model=pessimistic&origins=41.045524,29.007519&destinations=41.050044,29.029765&key=MYKEY

and here's the JSON Response:

    {
   "destination_addresses" : [
      "Ortaköy Mh., İstanbul Çevre Yolu, 34347 Beşiktaş/İstanbul, Turkey"
   ],
   "origin_addresses" : [
      "Cihannüma Mahallesi, Barbaros Blv. No:76, 34353 Beşiktaş/İstanbul, Turkey"
   ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "3.1 km",
                  "value" : 3052
               },
               "duration" : {
                  "text" : "4 mins",
                  "value" : 217
               },
               "duration_in_traffic" : {
                  "text" : "5 mins",
                  "value" : 295
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

Here's the Google Maps Screenshot for the specified time and destination

I don't know what causes that, but I would be appreciated if you could help.

Your departure time is in milliseconds rather than seconds, which is what the Distance Matrix API takes

https://developers.google.com/maps/documentation/distance-matrix/intro#departure-time

departure_time — The desired time of departure. You can specify the time as an integer in seconds since midnight, January 1, 1970 UTC. Alternatively, you can specify a value of now, which sets the departure time to the current time (correct to the nearest second).

Additionally, after converting that departure time to seconds, 1520301600, the date is actually Mar 6, 2018, 0200 UTC, which is 5am local time, not 5pm. Using 1520517600, which corresponds to Mar 8, 2018 5pm local time, the duration_in_traffic is 18 minutes:

https://maps.googleapis.com/maps/api/distancematrix/json?departure_time=1520517600&traffic_model=pessimistic&origins=41.045524,29.007519&destinations=41.050044,29.029765&key=YOUR_KEY

{
   "destination_addresses" : [
      "Ortaköy Mh., İstanbul Çevre Yolu, 34347 Beşiktaş/İstanbul, Turkey"
   ],
   "origin_addresses" : [
      "Cihannüma Mahallesi, Barbaros Blv. No:76, 34353 Beşiktaş/İstanbul, Turkey"
   ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "3.1 km",
                  "value" : 3052
               },
               "duration" : {
                  "text" : "4 mins",
                  "value" : 217
               },
               "duration_in_traffic" : {
                  "text" : "18 mins",
                  "value" : 1081
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM