Example #1
0
        /// <summary>
        /// Tries to geocode a simple waypoint.
        /// </summary>
        /// <param name="waypoint">The simple waypoint to geocode.</param>
        /// <param name="baseRequest">A base request that has the information need to perform a geocode, primarily a Bing Maps key.</param>
        /// <returns>A Task in which the simple waypoint will be geocoded.</returns>
        public static async Task TryGeocode(SimpleWaypoint waypoint, BaseRestRequest baseRequest)
        {
            if (waypoint != null && waypoint.Coordinate == null && !string.IsNullOrEmpty(waypoint.Address))
            {
                var request = new GeocodeRequest()
                {
                    Query        = waypoint.Address,
                    MaxResults   = 1,
                    BingMapsKey  = baseRequest.BingMapsKey,
                    Culture      = baseRequest.Culture,
                    Domain       = baseRequest.Domain,
                    UserIp       = baseRequest.UserIp,
                    UserLocation = baseRequest.UserLocation,
                    UserMapView  = baseRequest.UserMapView,
                    UserRegion   = baseRequest.UserRegion
                };

                try
                {
                    var r = await ServiceManager.GetResponseAsync(request).ConfigureAwait(false);

                    if (Response.HasResource(r))
                    {
                        var l = Response.GetFirstResource(r) as Location;

                        waypoint.Coordinate = new Coordinate(l.Point.Coordinates[0], l.Point.Coordinates[1]);
                    }
                }
                catch
                {
                    //Do nothing.
                }
            }
        }
        // common method for all BingREST request. Can return null value
        // takes parameter of type BaseRestRequest, gets the response and extracts the BingMapsRESTToolkit.Resource from the response. The method takes generic T : BingMapsRESTToolkit.Resource, to evaluate the resource according to request
        private async Task <T> getBingRESTResponseRequest <T>(BingMapsRESTToolkit.BaseRestRequest request) where T : BingMapsRESTToolkit.Resource
        {
            T   result   = null;
            var response = await ServiceManager.GetResponseAsync(request);

            if (response != null && response.ResourceSets != null && response.ResourceSets.Length > 0 &&
                response.ResourceSets[0].Resources != null && response.ResourceSets[0].Resources.Length > 0)
            {
                // result from Route Request is contained in BingMapsRESTToolkit.Route
                result = (T)Convert.ChangeType(response.ResourceSets[0].Resources[0], typeof(T));
            }
            return(result);
        }
Example #3
0
        /// <summary>
        /// Processes a REST requests that returns data.
        /// </summary>
        /// <param name="request">The REST request to process.</param>
        /// <returns>The response from the REST service.</returns>
        public static async Task <Response> GetResponseAsync(BaseRestRequest request)
        {
            Stream responseStream = null;

            if (request is ElevationRequest)
            {
                var r = request as ElevationRequest;

                if (r.Points != null && r.Points.Count > 50)
                {
                    //Make a post request when there are more than 50 points as there is a risk of URL becoming too large for a GET request.
                    responseStream = await ServiceHelper.PostStringAsync(new Uri(r.GetPostRequestUrl()), r.GetPointsAsString(), null);
                }
                else
                {
                    responseStream = await ServiceHelper.GetStreamAsync(new Uri(r.GetRequestUrl()));
                }
            }
            else if (request is ImageryRequest)
            {
                var r = request as ImageryRequest;

                r.GetMetadata = true;

                if (r.Pushpins != null && r.Pushpins.Count > 18)
                {
                    //Make a post request when there are more than 18 pushpins as there is a risk of URL becoming too large for a GET request.
                    responseStream = await ServiceHelper.PostStringAsync(new Uri(r.GetPostRequestUrl()), r.GetPushpinsAsString(), null);
                }
                else
                {
                    responseStream = await ServiceHelper.GetStreamAsync(new Uri(r.GetRequestUrl()));
                }
            }
            else
            {
                responseStream = await ServiceHelper.GetStreamAsync(new Uri(request.GetRequestUrl()));
            }

            if (responseStream != null)
            {
                var ser = new DataContractJsonSerializer(typeof(Response));
                var r   = ser.ReadObject(responseStream) as Response;
                responseStream.Dispose();
                return(r);
            }

            return(null);
        }
        /// <summary>
        /// Attempts to geocode a list of simple waypoints.
        /// </summary>
        /// <param name="waypoints">A list of simple waypoints to geocode.</param>
        /// <param name="baseRequest">A base request that has the information need to perform a geocode, primarily a Bing Maps key.</param>
        /// <returns>A Task in which a list of simple waypoints will be geocoded.</returns>
        internal static async Task GeocodeWaypoints(List <SimpleWaypoint> waypoints, BaseRestRequest baseRequest)
        {
            var geocodeTasks = new List <Task>();

            foreach (var wp in waypoints)
            {
                if (wp != null && wp.Coordinate == null && !string.IsNullOrEmpty(wp.Address))
                {
                    geocodeTasks.Add(TryGeocode(wp, baseRequest));
                }
            }

            if (geocodeTasks.Count > 0)
            {
                await Task.WhenAll(geocodeTasks);
            }
        }
        /// <summary>
        /// Tries to geocode a simple waypoint.
        /// </summary>
        /// <param name="waypoint">The simple waypoint to geocode.</param>
        /// <param name="baseRequest">A base request that has the information need to perform a geocode, primarily a Bing Maps key.</param>
        /// <returns>A Task in which the simple waypoint will be geocoded.</returns>
        internal static Task TryGeocode(SimpleWaypoint waypoint, BaseRestRequest baseRequest)
        {
            return(new Task(async() =>
            {
                if (waypoint != null && waypoint.Coordinate == null && !string.IsNullOrEmpty(waypoint.Address))
                {
                    var request = new GeocodeRequest()
                    {
                        Query = waypoint.Address,
                        MaxResults = 1,
                        BingMapsKey = baseRequest.BingMapsKey,
                        Culture = baseRequest.Culture,
                        Domain = baseRequest.Domain,
                        UserIp = baseRequest.UserIp,
                        UserLocation = baseRequest.UserLocation,
                        UserMapView = baseRequest.UserMapView,
                        UserRegion = baseRequest.UserRegion
                    };

                    try
                    {
                        var r = await ServiceManager.GetResponseAsync(request);

                        if (r != null && r.ResourceSets != null &&
                            r.ResourceSets.Length > 0 &&
                            r.ResourceSets[0].Resources != null &&
                            r.ResourceSets[0].Resources.Length > 0)
                        {
                            var l = r.ResourceSets[0].Resources[0] as Location;

                            waypoint.Coordinate = new Coordinate(l.Point.Coordinates[0], l.Point.Coordinates[1]);
                        }
                    }
                    catch
                    {
                        //Do nothing.
                    }
                }
            }));
        }
 /// <summary>
 /// Processes a REST requests that returns data.
 /// </summary>
 /// <param name="request">The REST request to process.</param>
 /// <param name="remainingTimeCallback">A callback function in which the estimated remaining time in seconds is sent.</param>
 /// <returns>The response from the REST service.</returns>
 public static async Task <Response> GetResponseAsync(BaseRestRequest request, Action <int> remainingTimeCallback)
 {
     return(await request.Execute(remainingTimeCallback).ConfigureAwait(false));
 }
 /// <summary>
 /// Processes a REST requests that returns data.
 /// </summary>
 /// <param name="request">The REST request to process.</param>
 /// <returns>The response from the REST service.</returns>
 public static async Task <Response> GetResponseAsync(BaseRestRequest request)
 {
     return(await request.Execute(null).ConfigureAwait(false));
 }
        /// <summary>
        /// Attempts to geocode a list of simple waypoints.
        /// </summary>
        /// <param name="waypoints">A list of simple waypoints to geocode.</param>
        /// <param name="baseRequest">A base request that has the information need to perform a geocode, primarily a Bing Maps key.</param>
        /// <returns>A Task in which a list of simple waypoints will be geocoded.</returns>
        public static async Task TryGeocodeWaypoints(List <SimpleWaypoint> waypoints, BaseRestRequest baseRequest)
        {
            var geocodeTasks = new List <Task>();

            foreach (var wp in waypoints)
            {
                if (wp != null && wp.Coordinate == null && !string.IsNullOrEmpty(wp.Address))
                {
                    geocodeTasks.Add(TryGeocode(wp, baseRequest));
                }
            }

            if (geocodeTasks.Count > 0)
            {
                await ServiceHelper.WhenAllTaskLimiter(geocodeTasks).ConfigureAwait(false);
            }
        }
Example #9
0
 /// <summary>
 /// Processes a REST requests that returns data.
 /// </summary>
 /// <param name="request">The REST request to process.</param>
 /// <returns>The response from the REST service.</returns>
 public static async Task <Response> GetResponseAsync(BaseRestRequest request)
 {
     return(await request.Execute(null));
 }