This function decodes a string into a generic List of LatLong objects for simplicity and performance.
/// <summary>
/// decodes a string into a list of latlon objects
/// </summary>
/// <param name="encoded">encoded string</param>
/// <returns>list of latlon</returns>
public static List<LatLong> DecodeLatLong(string encoded)
{
List<LatLong> locs = new List<LatLong>();
int index = 0;
int lat = 0;
int lng = 0;
int len = encoded.Length;
while (index < len)
{
lat += decodePoint(encoded, index, out index);
lng += decodePoint(encoded, index, out index);
locs.Add(new LatLong((lat * 1e-5), (lng * 1e-5)));
}
return locs;
}
/// <summary>
/// decodes the cuurent chunk into a single integer value
/// </summary>
/// <param name="encoded">the complete encodered string</param>
/// <param name="startindex">the current position in that string</param>
/// <param name="finishindex">output - the position we end up in that string</param>
/// <returns>the decoded integer</returns>
private static int decodePoint(string encoded, int startindex, out int finishindex)
{
int b;
int shift = 0;
int result = 0;
do
{
//get binary encoding
b = Convert.ToInt32(encoded[startindex++]) - minASCII;
//binary shift
result |= (b & 0x1f) << shift;
//move to next chunk
shift += binaryChunkSize;
} while (b >= 0x20); //see if another binary value
//if negivite flip
int dlat = (((result & 1) > 0) ? ~(result >> 1) : (result >> 1));
//set output index
finishindex = startindex;
return dlat;
}