Question

Please see below reference about google direction api response.

https://developers.google.com/maps/documentation/directions/?csw=1#JSON

as you can see each step tag include a tag named polyline. And this tags include a sub tag named points. As I understand, this tag include all points that you need to draw this step of direction on the map. And as you can see the value is encoded. I'm not sure what it is encoded but in google describes the algorithm in below article:

https://developers.google.com/maps/documentation/utilities/polylinealgorithm?csw=1

Does any one has some codes for decoding this value to a List<LatLng> for using in monondorid?

Was it helpful?

Solution

I shared this topic because I searched many times to found my answer. Saboor Awan at below article describes how to encode it with c#:

http://www.codeproject.com/Tips/312248/Google-Maps-Direction-API-V3-Polyline-Decoder

and here is the code for using on the monodroid:

private List<LatLng > DecodePolylinePoints(string encodedPoints) 
{
    if (encodedPoints == null || encodedPoints == "") return null;
    List<LatLng> poly = new List<LatLng>();
    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);
            LatLng p = new LatLng(Convert.ToDouble(currentLat) / 100000.0,
                Convert.ToDouble(currentLng) / 100000.0);
            poly.Add(p);
        } 
    }
    catch (Exception ex)
    {
        //log
    }
    return poly;
}

Just need to replace location with LatLng.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top