API.Models.Connexionz.ConnexionzRoute.EncodePolyline C# (CSharp) Method

EncodePolyline() public static method

Encodes a collection of LatLongs into GoogleMaps-style polylines.
public static EncodePolyline ( IEnumerable points ) : string
points IEnumerable
return string
        public static string EncodePolyline(IEnumerable<LatLong> points)
        {
            var str = new StringBuilder();

            Action<int> encodeDiff = diff =>
            {
                int shifted = diff << 1;
                if (diff < 0)
                    shifted = ~shifted;

                int rem = shifted;
                while (rem >= 0x20)
                {
                    str.Append((char)((0x20 | (rem & 0x1f)) + 63));
                    rem >>= 5;
                }

                str.Append((char)(rem + 63));
            };

            int lastLat = 0;
            int lastLng = 0;
            foreach (var point in points)
            {
                int lat = (int)Math.Round(point.Lat * 1E5);
                int lng = (int)Math.Round(point.Lon * 1E5);

                encodeDiff(lat - lastLat);
                encodeDiff(lng - lastLng);

                lastLat = lat;
                lastLng = lng;
            }

            return str.ToString();
        }