Beispiel #1
0
        public static string GetLatLngCollectionStr(IEnumerable <LatLng> locationsCollection, int encodedPolylineThreshold = 3)
        {
            if (locationsCollection == null)
            {
                return(null);
            }

            int countOfItems = locationsCollection.Count();

            if (countOfItems >= encodedPolylineThreshold)
            {
                return(Constants.PATH_ENCODED_PREFIX + PolylineEncoder.EncodeCoordinates(locationsCollection));
            }
            else
            {
                var stringBuilder = new StringBuilder(countOfItems * 22);                 // normally latlng's are -40.454545,-90.454545 so I picked a "larger than average" of 22 digits.

                foreach (LatLng position in locationsCollection)
                {
                    if (stringBuilder.Length > 0)
                    {
                        stringBuilder.Append("|");
                    }

                    stringBuilder.Append(position.GetAsUrlParameter());
                }

                return(stringBuilder.ToString());
            }
        }
        private static string GetPathEncoded(Path currentPath)
        {
            IEnumerable <LatLng> latlngPoints;

            try { latlngPoints = currentPath.Points.Cast <LatLng>().ToList(); }
            catch (InvalidCastException ex) { throw new InvalidOperationException("Encountered a point specified as a location.  Encoding only supports all points in LatLng types.", ex); }

            string encodedValue = PolylineEncoder.EncodeCoordinates(latlngPoints);

            return(encodedValue);
        }
Beispiel #3
0
        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);
        }
Beispiel #4
0
        public void Encoded_SinglePoint()
        {
            StaticMapRequestAccessor accessor = new StaticMapRequestAccessor();

            LatLng zero = new LatLng(30.0, -60.0);

            accessor.Path = new Path(zero)
            {
                Encode = true
            };

            string expected = "path=enc:" + PolylineEncoder.EncodeCoordinates(new LatLng[] { zero });
            string actual   = accessor.GetPathsStr();

            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        /// Builds path uri parameter
        /// </summary>
        /// <returns></returns>
        private string GetPathsStr()
        {
            if (this.Paths == null || this.Paths.Count == 0)
            {
                return(null);
            }

            string[] pathParam      = new string[this.Paths.Count];
            int      pathParamIndex = 0;

            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            foreach (Path currentPath in this.Paths)
            {
                sb.Length = 0;

                if (currentPath.Color.Equals(Color.black) == false)
                {
                    sb.Append("color:").Append(GetColorEncoded(currentPath.Color, true));
                }

                if (currentPath.FillColor.Equals(Color.black) == false)
                {
                    if (sb.Length > 0)
                    {
                        sb.Append(Constants.PIPE_URL_ENCODED);
                    }
                    sb.Append("fillcolor:").Append(GetColorEncoded(currentPath.FillColor, false));
                }

                if (currentPath.Encode.GetValueOrDefault() == true)
                {
                    string encodedValue = GetPathEncoded(currentPath);

                    if (sb.Length > 0)
                    {
                        sb.Append(Constants.PIPE_URL_ENCODED);
                    }
                    sb.Append(Constants.PATH_ENCODED_PREFIX).Append(encodedValue);
                }
                else
                {
                    if (currentPath.Encode == null && currentPath.Points.Count > 10)
                    {
                        IEnumerable <LatLng> positionCollection;
                        if (ConvertUtil.TryCast <Location, LatLng>(currentPath.Points, out positionCollection) == true)
                        {
                            string encodedValue = PolylineEncoder.EncodeCoordinates(positionCollection);
                            if (sb.Length > 0)
                            {
                                sb.Append(Constants.PIPE_URL_ENCODED);
                            }
                            sb.Append(Constants.PATH_ENCODED_PREFIX).Append(encodedValue);
                        }
                    }

                    foreach (Location loc in currentPath.Points)
                    {
                        if (sb.Length > 0)
                        {
                            sb.Append(Constants.PIPE_URL_ENCODED);
                        }
                        sb.Append(loc.GetAsUrlParameter());
                    }
                }

skipster:
                pathParam[pathParamIndex++] = "path=" + sb.ToString();
            }

            return(string.Join("&", pathParam));
        }
        /// <summary>
        /// Builds path uri parameter
        /// </summary>
        /// <returns></returns>
        private string GetPathsStr()
        {
            if (this.Path == null)
            {
                return(null);
            }

            string[] pathParam      = new string[1];
            int      pathParamIndex = 0;

            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            Path currentPath = this.Path;

            //foreach (Path path in this.Path)
            {
                sb.Remove(0, sb.Length);

                if (currentPath.Color.Equals(Color.Empty) == false)
                {
                    if (currentPath.Color.IsNamedColor && Constants.IsExpectedNamedColor(currentPath.Color.Name))
                    {
                        sb.AppendFormat("color:{0}", currentPath.Color.Name.ToLowerInvariant());
                    }
                    else
                    {
                        sb.AppendFormat("color:0x{0:X8}", currentPath.Color.ToArgb());
                    }
                }

                if (currentPath.FillColor.Equals(Color.Empty) == false)
                {
                    if (sb.Length > 0)
                    {
                        sb.Append(Constants.PIPE_URL_ENCODED);
                    }
                    sb.AppendFormat("fillcolor:{0:X8}", currentPath.Color.ToArgb());
                }

                if (currentPath.Encode.GetValueOrDefault() == true)
                {
                    string encodedValue = GetPathEncoded(currentPath);

                    if (sb.Length > 0)
                    {
                        sb.Append(Constants.PIPE_URL_ENCODED);
                    }
                    sb.Append(Constants.PATH_ENCODED_PREFIX).Append(encodedValue);
                }
                else
                {
                    if (currentPath.Encode == null && currentPath.Points.Count > 10)
                    {
                        IEnumerable <LatLng> positionCollection;
                        if (ConvertUtil.TryCast <Location, LatLng>(currentPath.Points, out positionCollection) == true)
                        {
                            string encodedValue = PolylineEncoder.EncodeCoordinates(positionCollection);
                            if (sb.Length > 0)
                            {
                                sb.Append(Constants.PIPE_URL_ENCODED);
                            }
                            sb.Append(Constants.PATH_ENCODED_PREFIX).Append(encodedValue);
                        }
                    }

                    foreach (Location loc in currentPath.Points)
                    {
                        if (sb.Length > 0)
                        {
                            sb.Append(Constants.PIPE_URL_ENCODED);
                        }
                        sb.Append(loc.GetAsUrlParameter());
                    }
                }

skipster:
                pathParam[pathParamIndex++] = "path=" + sb.ToString();
            }

            return(string.Join("&", pathParam));
        }
        /// <summary>
        /// Builds path uri parameter
        /// </summary>
        /// <returns></returns>
        private string GetPathsStr()
        {
            if (this.Paths == null || this.Paths.Count == 0)
            {
                return(null);
            }

            var pathParam      = new string[this.Paths.Count];
            var pathParamIndex = 0;
            var stringBuilder  = new StringBuilder();

            foreach (Path currentPath in this.Paths)
            {
                stringBuilder.Length = 0;

                if (!currentPath.Color.IsUndefined)
                {
                    stringBuilder.Append("color:").Append(currentPath.Color.To32BitColorString());
                }

                if (!currentPath.FillColor.IsUndefined)
                {
                    if (stringBuilder.Length > 0)
                    {
                        stringBuilder.Append(Constants.PIPE_URL_ENCODED);
                    }
                }
                stringBuilder.Append("fillcolor:").Append(currentPath.FillColor.To32BitColorString());

                if (currentPath.Encode.GetValueOrDefault() == true)
                {
                    string encodedValue = GetPathEncoded(currentPath);

                    if (stringBuilder.Length > 0)
                    {
                        stringBuilder.Append(Constants.PIPE_URL_ENCODED);
                    }

                    stringBuilder.Append(Constants.PATH_ENCODED_PREFIX).Append(encodedValue);
                }
                else
                {
                    if (currentPath.Encode == null && currentPath.Points.Count > 10)
                    {
                        IEnumerable <LatLng> positionCollection;

                        if (ConvertUtil.TryCast <Location, LatLng>(currentPath.Points, out positionCollection) == true)
                        {
                            string encodedValue = PolylineEncoder.EncodeCoordinates(positionCollection);

                            if (stringBuilder.Length > 0)
                            {
                                stringBuilder.Append(Constants.PIPE_URL_ENCODED);
                            }

                            stringBuilder.Append(Constants.PATH_ENCODED_PREFIX).Append(encodedValue);
                        }
                    }

                    foreach (Location loc in currentPath.Points)
                    {
                        if (stringBuilder.Length > 0)
                        {
                            stringBuilder.Append(Constants.PIPE_URL_ENCODED);
                        }

                        stringBuilder.Append(loc.GetAsUrlParameter());
                    }
                }

                pathParam[pathParamIndex++] = "path=" + stringBuilder.ToString();
            }

            return(String.Join("&", pathParam));
        }