public void GetLatLong(string locationStr, out decimal?lat, out decimal?lng) { locationStr = HttpContext.Current.Server.UrlEncode(locationStr); lat = null; lng = null; GeoCode geoCode; try { HttpClient client = new HttpClient(); HttpResponseMessage response = client.GetAsync("https://maps.google.com/maps/api/geocode/json?key=" + AppConstants.GoogleMapKey + "&address=" + locationStr).Result; response.EnsureSuccessStatusCode(); string responseBody = response.Content.ReadAsStringAsync().Result; geoCode = JsonConvert.DeserializeObject <GeoCode>(responseBody); Result resultFirstOrDefault = geoCode.results?.FirstOrDefault(); if (resultFirstOrDefault != null) { lat = resultFirstOrDefault.geometry.location.lat; lng = resultFirstOrDefault.geometry.location.lng; } } catch (System.Exception ex) { geoCode = new GeoCode(); } }
public void GetLatLongResult(string locationStr, out Result resultFirstOrDefault) { locationStr = HttpContext.Current.Server.UrlEncode(locationStr); resultFirstOrDefault = null; GeoCode geoCode; try { using (GenericRestHttpClient <GeoCode, string> memberClient = new GenericRestHttpClient <GeoCode, string>("https://maps.google.com", "/maps/api/geocode/json?key=" + AppConstants.GoogleMapKey + "&address=" + locationStr, null)) //AIzaSyBIoVMQPWUr5IjMi5f3mexwOhCA17d093A { geoCode = AsyncContext.Run(() => memberClient.GetAsync()); resultFirstOrDefault = geoCode.results?.FirstOrDefault(); } } catch (System.Exception ex) { geoCode = new GeoCode(); } }