Search code examples
jsongoogle-directory-api

Why the first value in step json array returned by Google directions api is not consistent with other items in the step list?


Below is a Json response from Google-directions-api.

If we look at distance object value seems to be in meters and text is round off value in kilometer. But the first object doesn't follow this.

Similar is the case with duration. Value is in seconds and text is round off value in minutes. But the first object in the steps array is not consistent with the result.

I need a consistent data for my Application. What is wrong with this? Is there any solution?

Note: Not all responses are inconsistent but for some responses inconsistency is there.

Any Kind of suggestion will be greatly appreciated.

Thanks!

"steps" : [
                  {
                     "distance" : {
                        "text" : "20 m",
                        "value" : 20
                     },
                     "duration" : {
                        "text" : "1 min",
                        "value" : 6
                     },
                     "end_location" : {
                        "lat" : 25.5940352,
                        "lng" : 85.1373598
                     },
                     "html_instructions" : "Head \u003cb\u003ewest\u003c/b\u003e",
                     "polyline" : {
                        "points" : "uye{CwlsfOAf@"
                     },
                     "start_location" : {
                        "lat" : 25.5940333,
                        "lng" : 85.1375638
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "0.1 km",
                        "value" : 124
                     },
                     "duration" : {
                        "text" : "1 min",
                        "value" : 65
                     },
                     "end_location" : {
                        "lat" : 25.5944879,
                        "lng" : 85.1365761
                     },
                     "html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e toward \u003cb\u003eBangali Tola Bus Stand Rd\u003c/b\u003e/\u003cb\u003eMithapur Bus Stand Rd\u003c/b\u003e/\u003cb\u003ePatna - Aurangabad Rd\u003c/b\u003e",
                     "maneuver" : "turn-right",
                     "polyline" : {
                        "points" : "wye{CoksfOI?i@AM?GAA@A??@A@Ah@?DGhB"
                     },
                     "start_location" : {
                        "lat" : 25.5940352,
                        "lng" : 85.1373598
                     },
                     "travel_mode" : "DRIVING"
                  },
                  {
                     "distance" : {
                        "text" : "1.1 km",
                        "value" : 1096
                     },
                     "duration" : {
                        "text" : "5 mins",
                        "value" : 325
                     },
                     "end_location" : {
                        "lat" : 25.5849493,
                        "lng" : 85.1338281
                     },
                     "html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e onto \u003cb\u003eBangali Tola Bus Stand Rd\u003c/b\u003e/\u003cb\u003eMithapur Bus Stand Rd\u003c/b\u003e/\u003cb\u003ePatna - Aurangabad Rd\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003eContinue to follow Mithapur Bus Stand Rd/Patna - Aurangabad Rd\u003c/div\u003e",
                     "maneuver" : "turn-left",
                     "polyline" : {
                        "points" : "q|e{CsfsfObBXxB`@tATD@p@Lv@LbEr@XDx@NhB\\~Dv@~@Np@LbDv@rDz@ZFjGpAbBXv@N"
                     },
                     "start_location" : {
                        "lat" : 25.5944879,
                        "lng" : 85.1365761
                     },
                     "travel_mode" : "DRIVING"
                  },]

Solution

  • If you look carefully, there is no inconsistency.

    First Object:

    distance: Value is in meters. value = 20 meter and hence text is 20 m

    duration: Value is in seconds. value = 6 seconds = 0.1 minutes. Rounding it off to non-zero integer value gives 1 minute and hence the text is 1 min

    Other objects follow the same pattern.

    Cheers!