Example #1
0
        /// <summary>
        /// Computes an overview of the driving route between two points using
        /// the Google Directions API.
        /// </summary>
        /// <exception cref="ApplicationException">Thrown if the Google Maps service
        /// does not return a route for any reason.</exception>
        /// <returns>The route info.</returns>
        /// <param name="apiKey">Google API key.</param>
        /// <param name="origin">Origin.</param>
        /// <param name="destination">Destination.</param>
        /// <param name="waypoints">Waypoints to visit.</param>
        public static async Task <RouteInfo> ComputeApproximateDrivingInfo(
            string apiKey,
            GeoCoordinates origin,
            GeoCoordinates destination,
            params GeoCoordinates[] waypoints)
        {
            DirectionRequest request = new DirectionRequest
            {
                Origin      = origin.ToGoogleLatLng(),
                Destination = destination.ToGoogleLatLng(),
                Mode        = TravelMode.driving,
                Waypoints   = waypoints.Length == 0 ?
                              null : new List <Location>(waypoints.Select(pt => new LatLng(pt.Latitude, pt.Longitude)))
            };

            DirectionRoute route = await GetRouteOrThrow(apiKey, request);

            return(new RouteInfo(
                       route.OverviewPolyline.ToGeoPolyline(),
                       TimeSpan.FromSeconds(
                           route.Legs.Sum(leg => leg.Duration.Value))));
        }