public static Decode ( string value ) : IEnumerable |
||
value | string | ASCII string |
return | IEnumerable |
public static IEnumerable<LatLng> Decode(string value)
{
//decode algorithm adapted from saboor awan via codeproject:
//http://www.codeproject.com/Tips/312248/Google-Maps-Direction-API-V3-Polyline-Decoder
//note the Code Project Open License at http://www.codeproject.com/info/cpol10.aspx
if(value == null || value == "") return new List<LatLng>(0);
char[] polylinechars = value.ToCharArray();
int index = 0;
int currentLat = 0;
int currentLng = 0;
int next5bits;
int sum;
int shifter;
List<LatLng> poly = new List<LatLng>();
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 point = new LatLng(
latitude: Convert.ToDouble(currentLat) / 100000.0,
longitude: Convert.ToDouble(currentLng) / 100000.0
);
poly.Add(point);
}
return poly;
}
public void decode_coords_1() { string value = "_p~iF~ps|U_ulLnnqC_mqNvxq`@"; LatLng[] expected = new LatLng[] { new LatLng(38.5, -120.2), new LatLng(40.7, -120.95), new LatLng(43.252, -126.453) }; IEnumerable <LatLng> actual2 = PolylineEncoder.Decode(value); LatLng[] actual = actual2.ToArray(); Assert.AreEqual(expected.Length, actual.Length, "Length"); for (int i = 0; i < actual.Length; i++) { Assert.AreEqual(expected[i].Latitude, actual[i].Latitude); Assert.AreEqual(expected[i].Longitude, actual[i].Longitude); } }