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)
{
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);
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)
{
}
return poly;
}
Here
Location
is simply a class with
Latitude and
Longitude attributes.