从 Google 地图路线 API 响应中标记高速公路

Mark highway from Google Maps Directions API response

我目前正在 Android 上使用 google 地图方向 API 进行项目。

当我发出任何指示请求时,我会收到诸如 this one

之类的响应

我实际上能够解码该点以获得要到达的后续 LatLng 坐标列表。现在我需要了解两个后续 LatLng 点之间的每条路径是高速公路(您必须付费才能到达那里)还是普通街道。

我不认为这个回复有任何方法可以做到这一点。我可以使用任何其他参数来显示此信息吗?或者我可以使用其他 api 吗?

{
   "geocoded_waypoints" : [
      {
         "geocoder_status" : "OK",
         "place_id" : "ChIJBVcXiwukOxMRaaVJcZxIGXQ",
         "types" : [ "locality", "political" ]
      },
      {
         "geocoder_status" : "OK",
         "place_id" : "ChIJ76RLkZW8OxMRpBS0rnKK0dI",
         "types" : [ "locality", "political" ]
      }
   ],
   "routes" : [
      {
         "bounds" : {
            "northeast" : {
               "lat" : 40.7912565,
               "lng" : 14.498954
            },
            "southwest" : {
               "lat" : 40.7461626,
               "lng" : 14.3675599
            }
         },
         "copyrights" : "Dati mappa ©2018 Google",
         "legs" : [
            {
               "distance" : {
                  "text" : "14,6 km",
                  "value" : 14606
               },
               "duration" : {
                  "text" : "21 min",
                  "value" : 1244
               },
               "end_address" : "80045 Pompei NA, Italia",
               "end_location" : {
                  "lat" : 40.7461626,
                  "lng" : 14.498954
               },
               "start_address" : "80059 Torre del Greco NA, Italia",
               "start_location" : {
                  "lat" : 40.7893898,
                  "lng" : 14.3675599
               },
               "steps" : [
                  {
                     "distance" : {
                        "text" : "0,3 km",
                        "value" : 260
                     },
                     "duration" : {
                        "text" : "1 min",
                        "value" : 71
                     },
                     "end_location" : {
                        "lat" : 40.7912565,
                        "lng" : 14.3694103
                     },
                     "html_instructions" : "Procedi in direzione \u003cb\u003enordest\u003c/b\u003e da \u003cb\u003eStrada Statale 18 Tirrena Inferiore\u003c/b\u003e/\u003cb\u003eSR18\u003c/b\u003e verso \u003cb\u003eTraversa I Vittorio Veneto\u003c/b\u003e",
                     "polyline" : {
                        "points" : "ut}wFgduvAe@c@WUKMa@]MOi@i@kCkCeAeA"
                     },
                     "start_location" : {
                        "lat" : 40.7893898,
                        "lng" : 14.3675599
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0,9 km",
                        "value" : 932
                     },
                     "duration" : {
                        "text" : "3 min",
                        "value" : 162
                     },
                     "end_location" : {
                        "lat" : 40.7846057,
                        "lng" : 14.3756157
                     },
                     "html_instructions" : "Svolta a \u003cb\u003edestra\u003c/b\u003e per rimanere su \u003cb\u003eStrada Statale 18 Tirrena Inferiore\u003c/b\u003e/\u003cb\u003eSR18\u003c/b\u003e",
                     "maneuver" : "turn-right",
                     "polyline" : {
                        "points" : "k`~wFyouvA~@}AhA{AdCcDhBaCd@o@tByCnAaBt@_ALQHMRUBCPOb@Qf@Gb@EVEr@KPGTG^WFILMNMLM`AeAp@s@RWLIJGNEJETAl@GXB"
                     },
                     "start_location" : {
                        "lat" : 40.7912565,
                        "lng" : 14.3694103
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "1,0 km",
                        "value" : 973
                     },
                     "duration" : {
                        "text" : "2 min",
                        "value" : 123
                     },
                     "end_location" : {
                        "lat" : 40.7908857,
                        "lng" : 14.3829714
                     },
                     "html_instructions" : "Svolta a \u003cb\u003esinistra\u003c/b\u003e e prendi \u003cb\u003eVia Cavallo\u003c/b\u003e",
                     "maneuver" : "turn-left",
                     "polyline" : {
                        "points" : "yv|wFsvvvACOCICICGO_@w@qAKOCGCICICISmAG_@CMEMCGAE_AoAkCgDoB}Bi@o@i@c@GGEECEYg@e@{@UYY[y@eAk@m@OQUWQKMEs@NMDSBK?IAECSOa@k@]a@ECMGSISQECg@q@IG{@qAAGQ_@"
                     },
                     "start_location" : {
                        "lat" : 40.7846057,
                        "lng" : 14.3756157
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0,2 km",
                        "value" : 202
                     },
                     "duration" : {
                        "text" : "1 min",
                        "value" : 35
                     },
                     "end_location" : {
                        "lat" : 40.7897602,
                        "lng" : 14.3848068
                     },
                     "html_instructions" : "Alla rotonda prendi la \u003cb\u003e1ª\u003c/b\u003e uscita e prendi lo svincolo \u003cb\u003eA3\u003c/b\u003e/\u003cb\u003eE45\u003c/b\u003e per \u003cb\u003eAutostrade\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003eStrada a pedaggio parziale\u003c/div\u003e",
                     "maneuver" : "roundabout-right",
                     "polyline" : {
                        "points" : "a~}wFqdxvA@C?C?E?E?C?ETe@b@y@x@}AbA}Af@u@"
                     },
                     "start_location" : {
                        "lat" : 40.7908857,
                        "lng" : 14.3829714
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "9,9 km",
                        "value" : 9941
                     },
                     "duration" : {
                        "text" : "7 min",
                        "value" : 402
                     },
                     "end_location" : {
                        "lat" : 40.7495621,
                        "lng" : 14.476954
                     },
                     "html_instructions" : "Mantieni la \u003cb\u003esinistra\u003c/b\u003e al bivio, segui le indicazioni per \u003cb\u003eE45\u003c/b\u003e/\u003cb\u003eSalerno\u003c/b\u003e/\u003cb\u003eReggio C.\u003c/b\u003e ed entra in \u003cb\u003eA3\u003c/b\u003e/\u003cb\u003eE45\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003eStrada a pedaggio\u003c/div\u003e",
                     "maneuver" : "fork-left",
                     "polyline" : {
                        "points" : "_w}wFapxvAV_@DEPQLIPIVKRGPEPCPAX?H@N@VFPDNHPHVRPPJLLRLRP\Vv@j@bBn@|BH`@DZBX@T@X?PAN?LCXCRCRGVGPGRKVMTKRMLGHIFIDGBI@I@GAIAGAGEIEEEEGEKEKAKCI?K?K?I@K?G@KBMBGDM@??APy@dBeDTc@Tg@Vg@zEoJJUFMTa@^u@p@iAb@w@x@qAn@{@rA_B~@aAbB}A`DqCfByAJKdBuAdA_A`A}@j@s@zAuBt@iAXg@Ta@Te@HQLYNa@Rw@^yAf@sBdAiERw@p@}Bz@eDl@gCr@yCj@eC`AcELe@Je@ViAt@}Cl@aCZwATeAp@{Ch@_CpAsFr@iCt@oCb@_B|@mDLi@J[H[tA_Fl@wBFUrA{E~@gDl@wB^wA~BoIDQFSdCyIf@iBVaANi@l@gCx@aEb@kBBMh@eCDYLc@XcATs@Ng@Lc@Na@Ja@Z{AJe@H_@F]XqA^wBVyAJq@Ns@Po@n@_CRq@p@oCz@aEZqADO?CFQrA{FBIDSBKJ_@r@oClA{EZsAFWFWfA{Ev@}Dx@wD?CH[?AF[b@kBlBkIdAcFdAmE|@uDZuAHe@XyAJw@H}@HmBF_DBiD@_@?a@HyHDmEHgEDu@Bk@Fi@Fw@^cCJk@Lk@ZgArAwDPg@p@aBdBiF?A@CFOh@{An@iBH[t@qB@GFOBIJ[^cA|@aC^cAjAwCt@kBfAoCjAuC@A`AaC~C}HpAcDn@aBv@wBt@sB`AsCbBcFBEJY?AJYfAsCzAcE|AgEvBaGx@aCzB}FlDgJXs@n@gB"
                     },
                     "start_location" : {
                        "lat" : 40.7897602,
                        "lng" : 14.3848068
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0,2 km",
                        "value" : 212
                     },
                     "duration" : {
                        "text" : "1 min",
                        "value" : 32
                     },
                     "end_location" : {
                        "lat" : 40.7482728,
                        "lng" : 14.4784155
                     },
                     "html_instructions" : "Prendi l'uscita \u003cb\u003ePompei\u003c/b\u003e verso \u003cb\u003ePompei\u003c/b\u003e/\u003cb\u003ePompei Ovest\u003c/b\u003e",
                     "maneuver" : "ramp-right",
                     "polyline" : {
                        "points" : "w{uwF}ojwARKBCBEL_@Vk@\q@`@{@Rk@Pc@HQDCDCFCDCF@D@D@@@FBVN"
                     },
                     "start_location" : {
                        "lat" : 40.7495621,
                        "lng" : 14.476954
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "1,0 km",
                        "value" : 991
                     },
                     "duration" : {
                        "text" : "3 min",
                        "value" : 169
                     },
                     "end_location" : {
                        "lat" : 40.7471718,
                        "lng" : 14.489806
                     },
                     "html_instructions" : "Svolta a \u003cb\u003esinistra\u003c/b\u003e e prendi \u003cb\u003eStrada Statale 18 Tirrena Inferiore\u003c/b\u003e/\u003cb\u003eSR18\u003c/b\u003e",
                     "maneuver" : "turn-left",
                     "polyline" : {
                        "points" : "usuwFcyjwATwAPuAJw@JaAJcAPcBDc@Hk@Lm@TcA`@iBr@cDN{@j@qD?GFcAB{@IaBq@yNSiEEkABWAeA"
                     },
                     "start_location" : {
                        "lat" : 40.7482728,
                        "lng" : 14.4784155
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0,5 km",
                        "value" : 520
                     },
                     "duration" : {
                        "text" : "2 min",
                        "value" : 102
                     },
                     "end_location" : {
                        "lat" : 40.74907,
                        "lng" : 14.4952359
                     },
                     "html_instructions" : "Alla rotonda prendi la \u003cb\u003e2ª\u003c/b\u003e uscita e prendi \u003cb\u003eVia Plinio\u003c/b\u003e",
                     "maneuver" : "roundabout-right",
                     "polyline" : {
                        "points" : "yluwFi`mwA@??A@A?A@??A?A?A?A?A?A?A?A?AA??A?AA??AA??AA??AA?A?A?A?A??@A?KQGCeAsCQi@kCuGMs@OaA?Cg@kEi@kEEa@"
                     },
                     "start_location" : {
                        "lat" : 40.7471718,
                        "lng" : 14.489806
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0,2 km",
                        "value" : 198
                     },
                     "duration" : {
                        "text" : "1 min",
                        "value" : 62
                     },
                     "end_location" : {
                        "lat" : 40.7493801,
                        "lng" : 14.4975391
                     },
                     "html_instructions" : "Continua su \u003cb\u003eVia Roma\u003c/b\u003e",
                     "polyline" : {
                        "points" : "uxuwFgbnwAEW?EKu@Kw@KaAGeACa@Aa@EsB"
                     },
                     "start_location" : {
                        "lat" : 40.74907,
                        "lng" : 14.4952359
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0,3 km",
                        "value" : 324
                     },
                     "duration" : {
                        "text" : "1 min",
                        "value" : 78
                     },
                     "end_location" : {
                        "lat" : 40.7466072,
                        "lng" : 14.4987269
                     },
                     "html_instructions" : "Svolta a \u003cb\u003edestra\u003c/b\u003e e prendi \u003cb\u003eVia Vittorio Emanuele\u003c/b\u003e",
                     "maneuver" : "turn-right",
                     "polyline" : {
                        "points" : "szuwFspnwAlC{@zAc@rC_ArBm@ZMZQ"
                     },
                     "start_location" : {
                        "lat" : 40.7493801,
                        "lng" : 14.4975391
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "53 m",
                        "value" : 53
                     },
                     "duration" : {
                        "text" : "1 min",
                        "value" : 8
                     },
                     "end_location" : {
                        "lat" : 40.7461626,
                        "lng" : 14.498954
                     },
                     "html_instructions" : "Continua su \u003cb\u003eVia Antonio Morese\u003c/b\u003e",
                     "polyline" : {
                        "points" : "iiuwFaxnwAdAc@RG"
                     },
                     "start_location" : {
                        "lat" : 40.7466072,
                        "lng" : 14.4987269
                     },
                     "travel_mode" : "DRIVING"
                  }
               ],
               "traffic_speed_entry" : [],
               "via_waypoint" : []
            }
         ],
         "overview_polyline" : {
            "points" : "ut}wFgduvAkBeBiGkG~@}AhA{AnFeHzCiEdCaDn@y@POb@QjAMjAQf@OdA}@`CgC`@a@ZM`@Gl@GXBGYGQgAqBOWGSWwAUcAaAuA{FeHi@o@i@c@MM]m@{@uAoCaDUWQKMEs@Na@HUAYS_AmASKg@[m@u@eAyASg@@G?Ux@_Bx@}AbA}A~@uAVW^Sj@Sb@Ij@AXBh@L`@Rh@d@X`@^p@bAzCx@~CHt@Bn@EhAGf@Oh@Sj@Yh@UVSLQDQ?QCQKKMKWEm@Bi@Nc@P{@zBiEtGuMnBoD|AiCbC{CbD_DhGkFpBaBfC}BfCiDnAqBbAsBb@yAfAmExAaGlBcHnEkRfBsHpC{LzBsJhByGzBsIvJ}]rGoU~@kD|@qDjCaMR}@n@wBx@oCx@_Ex@iEb@kC`@cBbAqDp@oCz@aE`@aB~A{GhAoExB_JfA{Ev@}Dx@{Dt@eDrDoPbCcKd@{Bd@qCRkDJiIPiQN}FJuAf@{DXwAZgArAwDbAiCnB_GrDqKhEaLjEsKbJeUlBkFdDwJZ{@xJaXtD_KfE{Kn@gBRKFId@kA~@mBd@oANULGLATHVNTwA\mCn@mGVyAv@mDbA_Fj@yDJ_C{@{QYuGBWAeA@??A@C@GEQEAC?A?MOGCeAsC}C_I]uBg@oEu@kGWmBSgCEcAEsBlC{@nFcBnC{@tB}@"
         },
         "summary" : "A3/E45",
         "warnings" : [],
         "waypoint_order" : []
      }
   ],
   "status" : "OK"
}

您可以在Directions Request中设置可选参数...&avoid=tolls...(或highways或两者:avoid=tolls|highways),例如:

https://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&avoid=tolls&mode=bicycling&key=[YOUR_KEY]

不用那种路也能得到响应。

更新

this answer of Rishi Singh:对于收费公路,您可以在steps节点的html_instructions子节点中找到“toll”(或“toll road”)标签作为回应 JSON,如:

...
"steps": [
{
          "distance": {
            "text": "42.5 mi",
            "value": 68323
          },
          "duration": {
            "text": "38 mins",
            "value": 2254
          },
          "end_location": {
            "lat": 42.4352772,
            "lng": -87.95487469999999
          },
          "html_instructions": "Merge onto <b>I-94 E</b><div style=\"font-size:0.9em\">Partial toll road</div><div style=\"font-size:0.9em\">Entering Illinois</div>",
          "maneuver": "merge",
          "polyline": {
...

因此,您不仅要标记收费路段,还要标记整个 "step" 收费公路。

另外,看看 answer of Emad Dehnavi

更新 2

也许可以为带有 "Partial toll road" 标签的步骤创建 "subrequest" 并获得更准确的响应 "Toll road" 部分...或将响应与 &avoid=tolls 组合并且没有那个限制并以这种方式获得通行费。

更新 3

还有另一种解决方法:您可以使用 Google 地图 Styled Map 并设置自定义样式(例如,红色代表有控制通道的道路,蓝色代表高速公路),如图所示:

然后在道路上获得分数并使用 答案中的方法来测试程式化地图上那个点的颜色,此外 JSON 分析,也许你确定 highway/toll 道路更准确地说。