Get address from Lat LOng using Google maps api

Anonymous
2023-07-28T16:47:01.0566667+00:00

 public async Task<IActionResult> ReverseGeocode([FromBody] MyGeoData data)
        {

            string BingMapsApiKey = "Au7sMtQzyQZRzuQ2krOIbZg8j2MGoHzzOJAmVym6vQjHq_BJ8a1YQGX3iCosFh8u";

            using (HttpClient client = new HttpClient())
            {
                string url = $"https://dev.virtualearth.net/REST/v1/Locations/{data.latitude},{data.longitude}?o=json&key={BingMapsApiKey}";
                var response = client.GetAsync(url).Result;

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

                // Parse the JSON response to extract the address
                dynamic data1 = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
                string address = data1.resourceSets[0].resources[0].address.formattedAddress;
                ViewData["latitude"] = data.latitude;
                ViewData["longitude"] = data.longitude;
                ViewData["address"] = address;

                return Ok(new
                {
                    latitude = data.latitude,
                    longitude = data.longitude,
                    address = address
                });
            }
        }

MYLONGLAT.txt

Can someone help me to find the below formatted address using google maps api. Attached is the file and I want to fetch the "formatted_address" . https://maps.googleapis.com/maps/api/geocode/json?latlng=17.7451253,83.3366575&key=MYKEY

"formatted_address" : "2-23-34, Sector- 6, MVP Colony, Visakhapatnam, Andhra Pradesh 530017, India"

Developer technologies ASP.NET ASP.NET Core
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-07-31T06:39:41.1966667+00:00

    Hi @KALYANA ALLAM

    Just like what you did using the virtualearth API, refer to you provide code, change the request URL to google API format:

    https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=YOUR_API_KEY
    

    Set the latitude, longitude and change the API key to your actual google map API key. The response like this:

    User's image

    After getting the response json string, you can also use JsonConvert.DeserializeObject method to convert the json result to an object, and then get the formatted_address value.

    More detail information about using google map API, see Reverse geocoding request and response (address lookup).


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.