Пример #1
0
        /// <summary>
        /// Sends the API request to generate a list of geo points snapped to a road from the specified <paramref name="positions"/> list of unsnapped points.
        /// <param name="positions">A list of unsnapped geo points</param>
        /// <param name="shouldInterpolate">Determines whether to return additional points to smoothly follow the geometry of the road</param>
        /// <returns>Response model with the snapped positions</returns>
        /// </summary>
        public async Task <OperationResult <RoadsResponse> > RequestSnappedLocations(IList <Position> positions, bool shouldInterpolate)
        {
            if (positions == null || positions.Count == 0)
            {
                return(OperationResult <RoadsResponse> .AsFailure("Please provide at least one position"));
            }

            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("Content-Type", "application/json;charset=utf-8");


                var fullURLString = RoadsUrlFactory.BuildSnapToRoadsUrl(_apiKey, positions, shouldInterpolate);

                try
                {
                    var responseString = await client.GetStringAsync((fullURLString));

                    return(await ProcessResponse(responseString));
                }
                catch (Exception ex)
                {
                    return(OperationResult <RoadsResponse> .AsFailure("Error communicating with Google Snap To Roads API: " + ex.Message));
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Sends the API request to generate a list of geo points snapped to a road from the specified <paramref name="positions"/> list of unsnapped points.
        /// <param name="positions">A list of unsnapped geo points</param>
        /// <param name="shouldInterpolate">Determines whether to return additional points to smoothly follow the geometry of the road</param>
        /// <returns>Response model with the snapped positions</returns>
        /// </summary>
        public async Task <OperationResult <RoadsResponse> > RequestSnappedLocations(IList <Position> positions, bool shouldInterpolate = false)
        {
            if (positions == null || positions.Count == 0)
            {
                return(OperationResult <RoadsResponse> .AsFailure("Please provide at least one position"));
            }

            using (var client = new HttpClient())
            {
                AcceptJsonResponse(client);
                var fullURLString = RoadsUrlFactory.BuildSnapToRoadsUrl(_apiKey, positions, shouldInterpolate);

                try
                {
                    var response = await client.GetAsync(fullURLString);

                    if (!response.IsSuccessStatusCode)
                    {
                        return(OperationResult <RoadsResponse> .AsFailure(response.ReasonPhrase));
                    }

                    var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                    return(await ProcessResponse(content));
                }
                catch (Exception ex)
                {
                    return(OperationResult <RoadsResponse> .AsFailure("Error communicating with Google Snap To Roads API: " + ex.Message));
                }
            }
        }