代码之家  ›  专栏  ›  技术社区  ›  sadesoda

Google Maps Distance Matrix API提供了超短持续时间的in\u流量结果,而且结果与Google Maps不同

  •  1
  • sadesoda  · 技术社区  · 8 年前

    我向Google Maps Distance Matrix API发出请求,以获取今天下午伊斯坦布尔贝西克塔斯和博斯普鲁斯桥两地之间的duration\u in\u交通数据,我已将出发时间设置为2018年3月6日17:00:00。它告诉我,这需要5分钟,这简直是不可能的,至少需要20分钟。谷歌地图中的结果也不同。

    以下是我使用的URL: 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

    下面是JSON响应:

        {
       "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

    我不知道是什么原因造成的,但如果你能帮忙,我将不胜感激。

    1 回复  |  直到 8 年前
        1
  •  1
  •   Preston    8 年前

    您的出发时间是以毫秒为单位的,而不是以秒为单位的,这就是距离矩阵API所采用的时间

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

    department\u time所需的出发时间。可以在中将时间指定为整数 秒数 自UTC 1970年1月1日午夜起。或者,您可以指定值now,该值将出发时间设置为当前时间(精确到最近的秒)。

    此外,将出发时间转换为秒1520301600后,日期实际上是2018年3月6日0200 UTC,这是当地时间上午5点,而不是下午5点。使用当地时间2018年3月8日下午5点对应的1520517600,持续时间为18分钟:

    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"
    }