Get address from latitude and longitude using google map api


We can find address components Country, State, Postal code etc. from latitude and longitude using google map api.
For this google provide a service. We have to call this service using post method. Service return result in two formats xml or json based on the parameter passed as xml or json.

using System.Xml;
public void GetAddressFromLatLon(Decimal Latitude, Decimal Longitude)
{
    string result = null;
    System.Net.WebClient webClient = new System.Net.WebClient();
    var apiUrl = "https://maps.googleapis.com/maps/api/geocode/xml?latlng=" + Latitude + "," + Longitude + "&sensor=false";
    var searchRequest = "";
    webClient.Headers.Add("content-type", "application/x-www-form-urlencoded");
    result = webClient.UploadString(apiUrl, searchRequest);
    XmlDocument xbook = new XmlDocument();
    xbook.LoadXml(result);
    string CountryCode = string.Empty;
    string CountryName = string.Empty;
    string StateCode = string.Empty;
    string stateName = string.Empty;
    foreach (XmlNode node in xbook.DocumentElement.ChildNodes)
    {
        if (node.Name == "result")
        {
            foreach (XmlNode node1 in node)
            {
                if (node1.Name == "address_component")
                {
                    foreach (XmlNode node2 in node1)
                    {
                        if (node2.Name == "type" && node2.InnerText == "country")
                        {
                            foreach (XmlNode node3 in node1)
                            {
                                if (node3.Name == "long_name")
                                {
                                    CountryName = node3.InnerText;
                                }
                                else if (node3.Name == "short_name")
                                {
                                    CountryCode = node3.InnerText;
                                }
                            }
                        }
                        if (node2.Name == "type" && node2.InnerText == "administrative_area_level_1")
                        {
                            foreach (XmlNode node3 in node1)
                            {
                                if (node3.Name == "long_name")
                                {
                                    stateName = node3.InnerText;
                                }
                                else if (node3.Name == "short_name")
                                {
                                    StateCode = node3.InnerText;
                                }
                            }
                        }
                    }
                }
            }
            break;
        }
    }
}


Call the service by passing Latitude and Longitude.

Decimal Latitude = Convert.ToDecimal(50.95846878);
Decimal Longitude = Convert.ToDecimal(6.97181002);
GetAddressFromLatLon(Latitude, Longitude);