Google Maps - Trace Route Itinerary Direction API

LucasPjoLucasPjo FRMember
edited February 2017 in Cross Platform with Xamarin

Hi ,

I need trace an itinerary route. I use the Google Direction api for get all point Lat/Lng.. The problème is : # Roads are not a straight line. But the Route is a straightLine

How to do this ? Thx to you ! :smile:

Best Answers

  • LucasPjoLucasPjo FR
    Accepted Answer

    **I found the solution ! **

    Look this json : https://maps.googleapis.com/maps/api/directions/json?origin=Parc Naturel régional du Gâtinais Français, Maison du Parc 20 boulevard du Maréchal Lyautey, 91490 Milly-la-Forêt&destination=Chemin du Rûcher, 77630 Arbonne-la-Forêt&mode=driving&key=123123123

    The json return the latitude and longitude of all position. for get the curve of the road you can use this =

    "polyline" : { "points" : "k_mfHm_aNMyEA[C}[email protected]?I?I?GB[" },

    Polyline has been Encoded by google and return multiple point(lat,long) after decode.

    **DECODE POLYLINE CROSSPLATFORM **

       >  private List<Position> DecodePolylinePoints(string encodedPoints)
    
        {
            if (encodedPoints == null || encodedPoints == "") return null;
            List<Position> poly = new List<Position>();
            char[] polylinechars = encodedPoints.ToCharArray();
            int index = 0;
            int currentLat = 0;
            int currentLng = 0;
            int next5bits;
            int sum;
            int shifter;
            try
            {
                while (index < polylinechars.Length)
                {
                    // calculate next latitude
                    sum = 0;
                    shifter = 0;
                    do
                    {
                        next5bits = (int)polylinechars[index++] - 63;
                        sum |= (next5bits & 31) << shifter;
                        shifter += 5;
                    } while (next5bits >= 32 && index < polylinechars.Length);
                    if (index >= polylinechars.Length)
                        break;
                    currentLat += (sum & 1) == 1 ? ~(sum >> 1) : (sum >> 1);
                    //calculate next longitude
                    sum = 0;
                    shifter = 0;
                    do
                    {
                        next5bits = (int)polylinechars[index++] - 63;
                        sum |= (next5bits & 31) << shifter;
                        shifter += 5;
                    } while (next5bits >= 32 && index < polylinechars.Length);
                    if (index >= polylinechars.Length && next5bits >= 32)
                        break;
                    currentLng += (sum & 1) == 1 ? ~(sum >> 1) : (sum >> 1);
                    Position p = new Position(Convert.ToDouble(currentLat) / 100000.0,
                        Convert.ToDouble(currentLng) / 100000.0);
                    poly.Add(p);
                }
            }
            catch (Exception ex)
            {
                //log
            }
            return poly;
        }
    

    Enjoy :)

Answers

Sign In or Register to comment.