iphonecocoa-touchgoogle-mapsmap-directions

How to show "Turn left" "Turn right" messages on the route highlighted between two places on Google map?


I'm developing an application in which I need to display a message showing where to turn after user clicks a position on route highlighted with the help of directions on Google map.

There's this Groute class which provides much of the information like distance, duration between two places, but it doesn't give information about direction.


Solution

  • From the GRoute you can use .getStep(i) to obtain the GStep object for each step in the GRoute. .getNumSteps() tells you how many GSteps are in a GRoute.

    For each GStep you can call .getDescriptionHtml() which will return the formatted description for that individual step. Like "Take the 1st right onto A5099/Coronation St" or "At the roundabout, take the 3rd exit onto A6/Garstang Rd heading to Preston".

    To relate the GStep to a specific vertex of the polyline, scan through all the GSteps looking for the last one with a getPolylineIndex() that's less than or equal to the specified vertex. Like this:

          var v = 66; // The vertex you are looking for
    
          var targetStep = route.getStep(0);
          for (var j=0; j<route.getNumSteps(); j++) {
            var step = route.getStep(j);
            if (step.getPolylineIndex() < v) targetStep = step;
          }
          GLog.writeHtml(targetStep.getDescriptionHtml());
    

    Don't attempt to parse the .getDescriptionHtml() because the details of the structure change from time to time and vary depending on the country of the route and the host language or locale.