Exemplo n.º 1
0
        public async Task <RouteEM> GetRoute(Coordinate origin, Coordinate destination, IEnumerable <Coordinate> waypoints)
        {
            var result = new RouteEM();

            var directionRequest = new DirectionsRequest
            {
                Key               = GoogleConfig.ApiKey,
                Origin            = origin.ToLocation(),
                Destination       = destination.ToLocation(),
                Waypoints         = waypoints.Select(p => p.ToLocation()).ToArray(),
                Language          = Language.Russian,
                OptimizeWaypoints = false
            };

            var directionResponse = await GoogleMaps.Directions.QueryAsync(directionRequest);

            result.Status = (Status)directionResponse.Status;

            var firstRoute = directionResponse.Routes.FirstOrDefault();

            if (firstRoute != null)
            {
                foreach (var leg in firstRoute.Legs)
                {
                    result.Legs.Add(LegFromGoogleLeg(leg));
                }
            }

            return(result);
        }
        public async Task <RouteAM> FromExternalRoute(AddressAM origin, AddressAM destination, WaypointsAM waypoints, RouteEM externalRoute)
        {
            var result = new RouteAM {
                Comment = waypoints.Comment
            };

            var routePoints = new List <AddressAM> {
                origin, destination
            };

            routePoints.AddRange(waypoints.Points);
            routePoints = routePoints.Distinct().ToList();

            foreach (var externalLeg in externalRoute.Legs)
            {
                var routeLegKind = RouteLegKind.Transportation;
                if (externalLeg.Equals(externalRoute.Legs.First()))
                {
                    routeLegKind = RouteLegKind.Feed;
                }

                if (externalLeg.Equals(externalRoute.Legs.Last()))
                {
                    routeLegKind = RouteLegKind.WayBack;
                }

                var startAddress = await AddressService.GetNearestAddress(externalLeg.StartCoordinate, routePoints);

                startAddress.AdjustedLatitude  = externalLeg.StartCoordinate.Latitude;
                startAddress.AdjustedLongitude = externalLeg.StartCoordinate.Longitude;

                var endAddress = await AddressService.GetNearestAddress(externalLeg.EndCoordinate, routePoints);

                endAddress.AdjustedLatitude  = externalLeg.EndCoordinate.Latitude;
                endAddress.AdjustedLongitude = externalLeg.EndCoordinate.Longitude;

                var leg = new RouteLegAM
                {
                    Kind         = routeLegKind,
                    StartAddress = startAddress,
                    EndAddress   = endAddress,
                    Distance     = externalLeg.Distance,
                    Duration     = externalLeg.Duration
                };

                result.Legs.Add(leg);
            }

            return(result);
        }
Exemplo n.º 3
0
        public async Task FromExternalRoute()
        {
            var rootAddress = new AddressAM
            {
                Latitude  = 55.55555,
                Longitude = 66.66666
            };

            var firstWaypointAddress = new AddressAM
            {
                Latitude  = 11.11111,
                Longitude = 22.22222
            };

            var secondWaypointAddress = new AddressAM
            {
                Latitude  = 33.33333,
                Longitude = 44.44444
            };

            var waypoints = new WaypointsAM
            {
                Points = new List <AddressAM>
                {
                    firstWaypointAddress,
                    secondWaypointAddress
                },
                Comment = "Машина на охраняемой парковке. Пароль:\"Нраииттьься\""
            };

            var rootCoordinate = new Coordinate {
                Latitude = 55.55000, Longitude = 66.66000
            };
            var firstWaypointCoordinate = new Coordinate {
                Latitude = 11.11000, Longitude = 22.22000
            };
            var secondWaypointCoordinate = new Coordinate {
                Latitude = 33.33000, Longitude = 44.44000
            };

            var externalRoute = new RouteEM
            {
                Status = External.Models.Enums.Status.Ok,
                Legs   = new List <LegEM>
                {
                    new LegEM
                    {
                        StartCoordinate = rootCoordinate,
                        EndCoordinate   = firstWaypointCoordinate,
                        Distance        = 3000,
                        Duration        = 1600
                    },
                    new LegEM
                    {
                        StartCoordinate = firstWaypointCoordinate,
                        EndCoordinate   = secondWaypointCoordinate,
                        Distance        = 35000,
                        Duration        = 18000
                    },
                    new LegEM
                    {
                        StartCoordinate = secondWaypointCoordinate,
                        EndCoordinate   = rootCoordinate,
                        Distance        = 38000,
                        Duration        = 19600
                    }
                }
            };

            Suite.AddressServiceMock
            .Setup(m => m.GetNearestAddress(rootCoordinate, It.IsAny <IEnumerable <AddressAM> >()))
            .ReturnsAsync(rootAddress);
            Suite.AddressServiceMock
            .Setup(m => m.GetNearestAddress(firstWaypointCoordinate, It.IsAny <IEnumerable <AddressAM> >()))
            .ReturnsAsync(firstWaypointAddress);
            Suite.AddressServiceMock
            .Setup(m => m.GetNearestAddress(secondWaypointCoordinate, It.IsAny <IEnumerable <AddressAM> >()))
            .ReturnsAsync(secondWaypointAddress);

            var result = await Suite.RouteService.FromExternalRoute(rootAddress, rootAddress, waypoints, externalRoute);

            Assert.Equal(3, result.Legs.Count);

            // Check feeding leg
            Assert.Equal(rootAddress, result.Legs[0].StartAddress);
            Assert.Equal(firstWaypointAddress, result.Legs[0].EndAddress);
            Assert.Equal(externalRoute.Legs[0].Distance, result.Legs[0].Distance);
            Assert.Equal(externalRoute.Legs[0].Duration, result.Legs[0].Duration);
            Assert.Equal(RouteLegKind.Feed, result.Legs[0].Kind);

            // Check transportation leg
            Assert.Equal(firstWaypointAddress, result.Legs[1].StartAddress);
            Assert.Equal(secondWaypointAddress, result.Legs[1].EndAddress);
            Assert.Equal(externalRoute.Legs[1].Distance, result.Legs[1].Distance);
            Assert.Equal(externalRoute.Legs[1].Duration, result.Legs[1].Duration);
            Assert.Equal(RouteLegKind.Transportation, result.Legs[1].Kind);

            // Check wayBacking leg
            Assert.Equal(secondWaypointAddress, result.Legs[2].StartAddress);
            Assert.Equal(rootAddress, result.Legs[2].EndAddress);
            Assert.Equal(externalRoute.Legs[2].Distance, result.Legs[2].Distance);
            Assert.Equal(externalRoute.Legs[2].Duration, result.Legs[2].Duration);
            Assert.Equal(RouteLegKind.WayBack, result.Legs[2].Kind);

            Assert.Equal(rootCoordinate.Latitude, result.Legs[0].StartAddress.AdjustedLatitude);
            Assert.Equal(rootCoordinate.Longitude, result.Legs[0].StartAddress.AdjustedLongitude);

            Assert.Equal(firstWaypointCoordinate.Latitude, result.Legs[1].StartAddress.AdjustedLatitude);
            Assert.Equal(firstWaypointCoordinate.Longitude, result.Legs[1].StartAddress.AdjustedLongitude);

            Assert.Equal(secondWaypointCoordinate.Latitude, result.Legs[2].StartAddress.AdjustedLatitude);
            Assert.Equal(secondWaypointCoordinate.Longitude, result.Legs[2].StartAddress.AdjustedLongitude);
        }