Click here to Skip to main content
12,631,365 members (31,342 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

24.7K views
9 bookmarked
Posted

Google Directions API Polyline Points Decoder in C#

, 9 Jan 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Google Directions API Polyline Points Decoder in C#
I have an algorithm for Google Directions API Polyline Points Decoder in C#. It may save you some development time.

Assuming that you have collected encoded point string from the Google Direction API Call.

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

Here Location is simply a class with Latitude and Longitude attributes.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

saboor awan
Software Developer (Senior)
Pakistan Pakistan
Expertise on Enterprise solutions, Mobile Apps & Services based applications on .NET platform. As Sr. Software Engineer, I have worked on variety of technologies like ASP.NET MVC, MS Windows Phone 7, WCF, and MS SharePoint 2010, MS Dynamics CRM.

You may also be interested in...

Pro

Comments and Discussions

 
GeneralThanks, saves me some coding time. Pin
toy4fun9-Feb-14 6:57
membertoy4fun9-Feb-14 6:57 
QuestionMessage Automatically Removed Pin
24-Sep-13 0:26
professionalRavi.Kumar02124-Sep-13 0:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161205.3 | Last Updated 9 Jan 2012
Article Copyright 2012 by saboor awan
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid