如何在Google Directions API调用中获取2个航点之间的距离

时间:2019-02-05 06:27:18

标签: json google-api directions

背景:

我目前正在使用Google Directions API这样计算往返距离:

A -> B -> C -> A

基本上总是3个位置。起点和终点相同,并且有2个航点。我用这个电话:

https://maps.googleapis.com/maps/api/directions/json?
mode=walking
&origin=41.445678,-72.018184   // Location A
destination=41.445678,-72.018184   // Location A
&waypoints=via:41.439271,-72.042239|via:41.414976,-72.018970   // Location B & C
&key=API_KEY

然后我得到了总距离:

$distance = $directions->routes[0]->legs[0]->distance->value / 1000;

问题:

现在,我正在尝试在同一个通话中也仅获得与B -> C的距离,基本上仅是2个航路点之间的距离。问题是,如果不进行单独的通话(因此要支付两次),我将无法解决该问题。

当我返回结果时,它看起来像这样:

{
   "geocoded_waypoints" : [
      {
         "geocoder_status" : "OK",
         "place_id" : "ChIJPSCqu8h05okRs9KY748Yquo",
         "types" : [ "establishment", "finance", "point_of_interest", "post_office" ]
      },
      {
         "geocoder_status" : "OK",
         "place_id" : "ChIJX8nJxlQL5okROb_esXXtTeA",
         "types" : [ "premise" ]
      },
      {
         "geocoder_status" : "OK",
         "place_id" : "ChIJLyy-zhUL5okRufacS3f2QDw",
         "types" : [ "premise" ]
      },
      {
         "geocoder_status" : "OK",
         "place_id" : "ChIJPSCqu8h05okRs9KY748Yquo",
         "types" : [ "establishment", "finance", "point_of_interest", "post_office" ]
      }
   ],
   "routes" : [
      {
         "bounds" : {
            "northeast" : {
               "lat" : 41.4456777,
               "lng" : -72.01399409999999
            },
            "southwest" : {
               "lat" : 41.4133016,
               "lng" : -72.0422671
            }
         },
         "copyrights" : "Map data ©2019 Google",
         "legs" : [
            {
               "distance" : {
                  "text" : "6.1 mi",
                  "value" : 9878
               },
               "duration" : {
                  "text" : "2 hours 3 mins",
                  "value" : 7356
               },
               "end_address" : "764 Colonel Ledyard Hwy, Ledyard, CT 06339, USA",
               "end_location" : {
                  "lat" : 41.4456777,
                  "lng" : -72.0181729
               },
               "start_address" : "764 Colonel Ledyard Hwy, Ledyard, CT 06339, USA",
               "start_location" : {
                  "lat" : 41.4456777,
                  "lng" : -72.0181729
               },
               "steps" : [
                  {
                     "distance" : {
                        "text" : "13 ft",
                        "value" : 4
                     },
                     "duration" : {
                        "text" : "1 min",
                        "value" : 4
                     },
                     "end_location" : {
                        "lat" : 41.4456444,
                        "lng" : -72.0181743
                     },
                     "html_instructions" : "Head \u003cb\u003esouth\u003c/b\u003e on \u003cb\u003eCT-117 S\u003c/b\u003e toward \u003cb\u003eCT-214 E\u003c/b\u003e",
                     "polyline" : {
                        "points" : "oz}{Fp`qvLF?"
                     },
                     "start_location" : {
                        "lat" : 41.4456777,
                        "lng" : -72.0181729
                     },
                     "travel_mode" : "WALKING"
                  },
                  {
                     "distance" : {
                        "text" : "1.1 mi",
                        "value" : 1830
                     },
                     "duration" : {
                        "text" : "21 mins",
                        "value" : 1286
                     },
                     "end_location" : {
                        "lat" : 41.4448953,
                        "lng" : -72.0388138
                     },
                     "html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eCT-214 W\u003c/b\u003e",
                     "maneuver" : "turn-right",
                     "polyline" : {
                        "points" : "gz}{Fp`qvLl@nCJd@VbAHZDRDNFPDPHTL^FNHPRd@Xn@\\v@DHHRDNBL@D@F@FBN@P?P@PAR?RAT?p@?Z?X@ZB\\BZBRF^FZFVDRPh@^hAPj@Lb@Px@Lh@H`@Jp@H`@Hn@BR@N@N?NANAPAPCLCTGVGTGLCJELOZIPGNGNCJENELCNGZEXCVE^Ej@APAd@Ad@?d@@n@?hACr@AZCZEb@OfBGd@Eb@Ef@CREZGj@Eb@CRA\\?`@?d@@pABdA@^FhBBZBZHh@N`AD^@RBT?R?LAL?HEZCREVEROn@EVMt@CNETCXEZIj@Gd@E`@CPCNETEPGPKTINMP"
                     },
                     "start_location" : {
                        "lat" : 41.4456444,
                        "lng" : -72.0181743
                     },
                     "travel_mode" : "WALKING"
                  },
                  {
                     "distance" : {
                        "text" : "0.5 mi",
                        "value" : 728
                     },
                     "duration" : {
                        "text" : "9 mins",
                        "value" : 548
                     },
                     "end_location" : {
                        "lat" : 41.4392546,
                        "lng" : -72.0422671
                     },
                     "html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e onto \u003cb\u003eWhalehead Rd\u003c/b\u003e",
                     "maneuver" : "turn-left",
                     "polyline" : {
                        "points" : "su}{FpauvL`@THDJBJBHBF@J@N@ZDVDR@f@D\\BV@V?XATAP?PA\\@V@VB`@Dd@Fn@HNBHBHBHBFDHFFDRNPNXTNNRPRNp@l@ZV`@^\\XJLLLHLHNDNBHHPFRFTHRFNBFBFDFDFFJJLX^PVX^"
                     },
                     "start_location" : {
                        "lat" : 41.4448953,
                        "lng" : -72.0388138
                     },
                     "travel_mode" : "WALKING"
                  },
                  {
                     "distance" : {
                        "text" : "2.2 mi",
                        "value" : 3595
                     },
                     "duration" : {
                        "text" : "46 mins",
                        "value" : 2735
                     },
                     "end_location" : {
                        "lat" : 41.4133016,
                        "lng" : -72.0189894
                     },
                     "html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e onto \u003cb\u003eSandy Hollow Rd\u003c/b\u003e",
                     "maneuver" : "turn-left",
                     "polyline" : {
                        "points" : "ir|{FdwuvLf@O\\M\\OZOFCFEJGFIHIFKJSL[Ra@|@sBXm@V_@HKJMDEBCJIHGZOr@[p@YXOn@_@PMJIFGFGDEBEDGBE@EDGBGRo@L_@Na@Rc@Pa@HQHMHQ|@aBTa@Tc@N]Rg@f@oAj@_BPk@HSDIJQDIBEJOX_@FIPOPOVUNIn@_@VOr@_@~@g@\\Sb@WTOb@]LKRQZ]RUFI`@g@LO`@g@X_@z@aAV[FIBCHKVUVSTSPMpA}@dAw@LIhAw@l@a@vAeAj@a@hA{@n@a@tAcAbAs@z@o@lAy@^YlAy@d@_@d@[pBwAb@[rA_AxDoCXQXOHE\\OHENEZKTEDAHAPC`@Eb@E|@EnAG\\CPALAJCJATEPEJEPIXO^Wl@a@\\Sd@[f@[~@k@pAy@NGJELGLCVE`AKpAMv@KNEJCTIHEHELKHGRS`@c@X[h@s@fA_BhAcBx@kA~@uAbAyAHKFKJWJYF]Hc@DOBWHk@@YBW"
                     },
                     "start_location" : {
                        "lat" : 41.4392546,
                        "lng" : -72.0422671
                     },
                     "travel_mode" : "WALKING"
                  },
                  {
                     "distance" : {
                        "text" : "2.3 mi",
                        "value" : 3721
                     },
                     "duration" : {
                        "text" : "46 mins",
                        "value" : 2783
                     },
                     "end_location" : {
                        "lat" : 41.4456777,
                        "lng" : -72.0181729
                     },
                     "html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e onto \u003cb\u003eCT-117 N\u003c/b\u003e",
                     "maneuver" : "turn-left",
                     "polyline" : {
                        "points" : "cpw{FteqvLo@Bu@Bc@@o@?c@Ai@C]Ca@Ee@Gi@K[GsA]cDw@o@OcKeCiAYy@SkCm@c@Eg@CaACiA@wC?YCUCYCYGo@Qi@QkA_@y@YqKoDaDiAe@Og@Oa@OUGUG[IWE]E_AOuCc@qCa@kDg@kASk@Iu@K[Ca@Cw@Ag@BW@Q@c@D{ANi@DO@SBS@O@S@M?a@?Y?[?MAQAYCK?q@G[CYAM?W?M?G@E?G?I@G?I@G@G@UBKB{A^WF[FSDI@MBK@K@_@@q@B_A@e@?[@W@[@K@SBMBOBQFG@SF[LSHKFKHe@Zy@n@y@p@c@`@[VGD_@Za@\\YTWNSL[N[LQFQF}@T_AVaAXgAXkAZaDx@a@LgAZeBd@k@JKFK@MBQ@Q@WBWAG?"
                     },
                     "start_location" : {
                        "lat" : 41.4133016,
                        "lng" : -72.0189894
                     },
                     "travel_mode" : "WALKING"
                  }
               ],
               "traffic_speed_entry" : [],
               "via_waypoint" : [
                  {
                     "location" : {
                        "lat" : 41.4392734,
                        "lng" : -72.0422423
                     },
                     "step_index" : 2,
                     "step_interpolation" : 0.9959595432411245
                  },
                  {
                     "location" : {
                        "lat" : 41.4149736,
                        "lng" : -72.0189503
                     },
                     "step_index" : 4,
                     "step_interpolation" : 0.05009490732581229
                  }
               ]
            }
         ],
         "overview_polyline" : {
            "points" : "oz}{Fp`qvLF?l@nCb@hB\\pAd@vAtA~CXz@Hd@@hAAvBHnBZfBhArDv@hDb@vC@~@KfA[fAg@jAWx@U|AKjACv@?dEEnAI~@c@xEW~BEp@?fAN`HFv@XjBFr@Bh@G`A_@nB]rB[fCSxAMb@Ud@MP`@TTHh@JvANdAHn@@rAEt@Bx@HnBXd@TpDzCfB~AVZN^\\dAXr@^h@j@v@X^f@Oz@]b@Sd@a@t@}AvAaD`@k@`@a@d@WdBu@hAo@\\WX[P[t@yBx@eBrByDb@eArAoDl@{An@_AbA_AjCyA|A{@x@g@p@i@n@o@jAwAnCeDl@o@pCsBjE{C`PgL|QsMzAw@XKp@QbAMnEWv@Ir@QxBsAjDwB`BaAXMd@IrCYfAQ`@Mj@_@t@w@bAoAjGeJlAeBRc@Rw@Ns@LcADq@eBFsA@mAE_AIoASoBe@aTgFeEaAkAIkCAqDCo@GiAYaQ{FqGyB_B_@eK{AyIqA}@G_B@iDXaBLsABuACsBOmAAaAHuCn@gARiBHyCDiAJ}@TgAh@_BjA}ArAeBvAq@d@o@\\m@ToA\\uGfBqJhCw@RYDc@Bo@@G?"
         },
         "summary" : "Sandy Hollow Rd and CT-117 N",
         "warnings" : [
            "Walking directions are in beta.    Use caution – This route may be missing sidewalks or pedestrian paths."
         ],
         "waypoint_order" : []
      }
   ],
   "status" : "OK"
}

有什么办法吗?我尝试使用steps进行操作,但问题是是否有掉头或驶入不同的街道等。这会增加步伐,所以我不知道在B -> C之间有10步或1步。

0 个答案:

没有答案