3

I'm currently working on a project using google maps Directions API on Android.

When i make any direction request i get a response such as this one

I'm actually able to decode the point's to get a List of subsequent LatLng coordinates to reach. Now i need to understand for each path between two subsequent LatLng points if it's an highway street(you had to pay to get there) or a normal street.

I don't think there's any way of doing so from this response. Is there any other parameter i can use to show this information? or maybe is there any other api i can use?

{
   "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"
}
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Luca Reccia
  • 578
  • 3
  • 16

1 Answers1

3

You can set optional parameter ...&avoid=tolls... (or highways or both: avoid=tolls|highways) in Directions Request, e.g.:

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

to get response without that kind of roads.

UPDATE

From this answer of Rishi Singh: for toll roads you can find "toll" (or "toll road") tag in html_instructions subnode of steps node in response JSON, like:

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

So you mark not only toll part, but whole "step" with toll road.

Also, take a look at this answer of Emad Dehnavi.

UPDATE 2

May be it's possible to create "subrequest" for step with "Partial toll road" tag and get more precisely response with totally "Toll road" part... Or combine responses with &avoid=tolls and without that restriction and get toll parts this way.

UPDATE 3

And there is one more workaround: you can use Google Maps Styled Map and set custom style (e.g. red color for road with controlled access and blue for highways) something like on picture:

Highways and Controlled-access roads

then get points on road and use approach like in this answer to test color on stylized map at that point in addition to JSON analyze, may be you determine highway/toll road more precisely.

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
  • 1
    Here is the point, I don't have to avoid those road. Actually i need them. I need to know which part of the route use a tolls or an highway road and which part don't. – Luca Reccia Feb 01 '18 at 15:22
  • Well, well. This could be a nice workaround! I'll try to use those information in the instructions. Thanks for the tip! – Luca Reccia Feb 01 '18 at 15:47
  • 1
    Good luck! May be it's possible to create "subrequest" for step with "Partial toll road" tag and get more precisely response with totally "Toll road" part... Or combine responses with `.&avoid=tolls` and without that restriction and get toll parts this way. – Andrii Omelchenko Feb 01 '18 at 15:50
  • hey i guess update 3 could be an even better solution than update 1. Thanks for your help, I really appreciate! – Luca Reccia Feb 02 '18 at 08:18
  • Yes, at the end i used update 1 as a compromise between performance and accuracy. It did work well, i will now accept it, thanks again – Luca Reccia Feb 02 '18 at 10:23
  • @AndriiOmelchenko, as I have sort of same requirements except that I am to avoid tolls and highways on 'walking' mode. But seems like 'avoid' keyword doesn't do the work. For instance, check this: https://www.google.com/maps/dir/58.6581804,14.9613078/Unnamed+Road,+591+97+Motala,+Sweden/@58.6612316,14.9657797,15.25z/data=!4m9!4m8!1m0!1m5!1m1!1s0x465be9660823fedf:0x77542799df531235!2m2!1d14.9642418!2d58.6643679!3e3 It doesn't avoid highway 50, if we call directions API with avoid. And calling the staticmap will be expensive for all the steps in all different alternative routes too. – Chanchal Raj Sep 01 '19 at 09:23
  • @ChanchalRaj I'm agree, but what is toll road for pedestrians? Also check the `warnings` tag of response: "...Use caution – This route may be missing sidewalks or pedestrian paths." - seems Google suggests that pedestrians can walk everywhere even along tolls... – Andrii Omelchenko Sep 02 '19 at 08:47