public static string EncodeCoordinates(IEnumerable<LatLng> coordinates) { double oneEFive = Convert.ToDouble(1e5); int plat = 0; int plng = 0; StringBuilder encodedCoordinates = new StringBuilder(); foreach(LatLng coordinate in coordinates) { // Round to 5 decimal places and drop the decimal int late5 = (int)(coordinate.Latitude * oneEFive); int lnge5 = (int)(coordinate.Longitude * oneEFive); // Encode the differences between the coordinates encodedCoordinates.Append(EncodeSignedNumber(late5 - plat)); encodedCoordinates.Append(EncodeSignedNumber(lnge5 - plng)); // Store the current coordinates plat = late5; plng = lnge5; } return encodedCoordinates.ToString(); }
public void encode_coords_1() { LatLng[] points = new LatLng[] { new LatLng(38.5, -120.2), new LatLng(40.7, -120.95), new LatLng(43.252, -126.453) }; string actual = PolylineEncoder.EncodeCoordinates(points); string expected = "_p~iF~ps|U_ulLnnqC_mqNvxq`@"; Assert.AreEqual(expected, actual); }