Ejemplo n.º 1
0
        private async Task <GeosearchResponse> SearchAsync(string query)
        {
            string requestUri = "https://atlas.microsoft.com/search/address/json" +
                                $"?subscription-key={_configuration.ApiKey}" +
                                $"&api-version={_configuration.ApiVersion}" +
                                $"&query={query}";

            using HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri);
            HttpResponseMessage response = await _httpClient.SendAsync(request);

            if (!response.IsSuccessStatusCode)
            {
                _logger.LogError("Error getting geo result: {statusCode}", response.StatusCode);
                return(new GeosearchResponse());
            }

            string stringResponse = await response.Content.ReadAsStringAsync();

            JsonSerializerOptions options = new JsonSerializerOptions()
            {
                PropertyNameCaseInsensitive = true
            };
            GeosearchResponse geoJsonResponse = JsonSerializer.Deserialize <GeosearchResponse>(stringResponse, options);

            if (geoJsonResponse == null)
            {
                _logger.LogError("Error getting geo results, response is null");
                return(new GeosearchResponse());
            }

            return(geoJsonResponse);
        }
Ejemplo n.º 2
0
        public async Task <(RouteResponse route, GeosearchResponse source, GeosearchResponse destination)> FindRouteAsync(string source, string destination)
        {
            GeosearchResponse geoSource = await SearchAsync(source);

            GeosearchResponse geoDestination = await SearchAsync(destination);

            if (source != null && destination != null)
            {
                return(await GetRouteAsync(new RouteRequest
                {
                    Departure = new Geopoint
                    {
                        Latitude = geoSource.results.FirstOrDefault().position.lat,
                        Longitude = geoSource.results.FirstOrDefault().position.lon
                    },
                    Destination = new Geopoint
                    {
                        Latitude = geoDestination.results.FirstOrDefault().position.lat,
                        Longitude = geoDestination.results.FirstOrDefault().position.lon
                    }
                }), geoSource, geoDestination);
            }

            return(new RouteResponse(), new GeosearchResponse(), new GeosearchResponse());
        }