예제 #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());
            }
        }
            public void ShouldReturnProperValue(DecodeTestData testData)
            {
                // arrange
                var sut = new PolylineEncoder(PolylineEncoding.Polyline5);

                // act
                var actualResult = sut.Decode(testData.Polyline);

                // assert
                actualResult.Should().BeEquivalentTo(testData.ExpectedResult);
            }
            public void EmptyString_ShouldReturnEmptyCollection()
            {
                // arrange
                var sut = new PolylineEncoder(PolylineEncoding.Polyline5);

                // act
                var actualResult = sut.Decode(string.Empty);

                // assert
                actualResult.Should().BeEmpty();
            }
예제 #4
0
        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);
        }
            public void NullString_ShouldReturnNullCollection()
            {
                // arrange
                var sut = new PolylineEncoder(PolylineEncoding.Polyline5);

                // act
                var actualResult = sut.Decode(null);

                // assert
                actualResult.Should().BeNull();
            }
예제 #6
0
            public void EmptyCollection_ShouldReturnEmptyString()
            {
                // arrange
                var sut = new PolylineEncoder(PolylineEncoding.Polyline5);

                // act
                string actualResult = sut.Encode(new GeoCoordinate[0]);

                // assert
                actualResult.Should().BeEmpty();
            }
예제 #7
0
            public void ShouldReturnProperValue(EncodeTestData testData)
            {
                // arrange
                var sut = new PolylineEncoder(PolylineEncoding.Polyline5);

                // act
                string actualResult = sut.Encode(testData.Coordinates);

                // assert
                actualResult.Should().Be(testData.ExpectedResult);
            }
예제 #8
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);
        }
        private static IEnumerable <LatLng> AssembleHQPolyline(DirectionRoute route)
        {
            var hdRoute = new List <LatLng>();

            foreach (var leg in route.Legs)
            {
                foreach (var step in leg.Steps)
                {
                    hdRoute.AddRange(PolylineEncoder.Decode(step.Polyline.Points));
                }
            }

            return(hdRoute);
        }
예제 #10
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);
        }
예제 #11
0
        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);
            }
        }
        private static IEnumerable <LatLng> AssembleOverviewPolyline(DirectionRoute route)
        {
            var line = route.OverviewPolyline;

            return(PolylineEncoder.Decode(line.Points));
        }
예제 #13
0
        /// <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));
        }
예제 #14
0
        /// <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));
        }
예제 #15
0
 static GeoPolyline ToGeoPolyline(this Polyline polyline)
 {
     return(new GeoPolyline(
                PolylineEncoder.Decode(polyline.Points)
                .Select(pt => new GeoCoordinates(pt.Latitude, pt.Longitude))));
 }
예제 #16
0
 public static GeoPolyline ToGeo(this Polyline polyline)
 {
     return(new GeoPolyline(from latLng in PolylineEncoder.Decode(polyline.Points) select latLng.ToGeo()));
 }
예제 #17
0
        /// <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));
        }