コード例 #1
0
        public async Task <ActionResult <GeoRootObject> > Get([FromQuery] QueryLatLngModel model)
        {
            string url = SetUrl(model);
            List <GeoLocationApiModel> places;

            using (var httpClient = new HttpClient())
            {
                using var response = await httpClient.GetAsync(url);

                if (response.IsSuccessStatusCode)
                {
                    using var content = response.Content;
                    var result = await content.ReadAsStringAsync();

                    if (result != null)
                    {
                        GeoRootObject root = JsonConvert.DeserializeObject <GeoRootObject>(result);
                        places = root.Results;
                    }
                    else
                    {
                        return(NotFound());
                    }
                }
                else
                {
                    return(BadRequest());
                }
            }
            return(Ok(places.FirstOrDefault()));
        }
コード例 #2
0
        private string SetUrl(QueryLatLngModel model)
        {
            UriBuilder uriBuilder = new UriBuilder(Constants.googleGeocodeUrl);
            var        query      = HttpUtility.ParseQueryString(uriBuilder.Query);

            query["address"] = model.Address;

            uriBuilder.Query = query.ToString() + "&key=" + Constants.apiKey;
            return(uriBuilder.ToString());
        }