示例#1
0
 private Location NextLocation(Location origin)
 {
     var next = RandomLocation();
     while (next.Name == origin.Name)
     {
         next = RandomLocation();
     }
     return next;
 }
示例#2
0
        private static Route MapRoute(Location origin, Location destination, DirectionsResponse result)
        {
            var points = result.Routes.First()
                .Legs.SelectMany(leg => leg.Steps)
                .Select(step => new RouteLeg(
                    new Position(step.StartLocation.Latitude, step.StartLocation.Longitude),
                    new Position(step.EndLocation.Latitude, step.EndLocation.Longitude),
                    step.Distance.Value))
                .ToArray();

            return new Route(origin, destination, points);
        }
示例#3
0
        public async Task<Route> PlanNewRoute(Location origin)
        {
            var destination = NextLocation(origin);
            var request = new DirectionsRequest
            {
                Origin = origin.Name,
                Destination = destination.Name,
                TravelMode = TravelMode.Driving
            };

            return await CallGoogleService(origin, request, destination);
        }
示例#4
0
 private static async Task<Route> CallGoogleService(Location origin, DirectionsRequest request, Location destination)
 {
     var count = 0;
     while (count < 3)
     {
         try
         {
             var result = await GoogleMaps.Directions.QueryAsync(request);
             if (result.Status == DirectionsStatusCodes.OK)
             {
                 return MapRoute(origin, destination, result);
             }
         }
         catch (Exception exception)
         {
             Console.WriteLine("Error while contacting the google service: " + exception.ToString());
         }
         count ++;
     }
     throw new InvalidOperationException("Could not contact the Google service after 3 times.");
 }