Tag Archives: get address with latitude and longitude google map

Get address from latitude and longitude using Bing map api

We can find address components Country, AddressLine, Postal code etc. from latitude and longitude using Bing map api.
For this Microsoft provide a Bing map rest api service. Call this service using HttpClient and get response in json format. To use Bing map rest api service BingMapKey required. Create Bing map key from the portal https://www.bingmapsportal.com/. To parse json we use JsonConvert class. Install Newtonsoft.Json reference from the Nuget packages.

using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
public void GetAddressFromLatLon(decimal Latitude, decimal Longitude)
{
    var apiURL = "http://dev.virtualearth.net/Rest/v1/Locations/";
    var searchRequest = Latitude + "," + Longitude + "?&key=BingMapKey";
    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri(apiURL);
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    HttpResponseMessage response = client.GetAsync(searchRequest).Result;
    string Country = string.Empty;
    string Address = string.Empty;
    if (response.IsSuccessStatusCode)
    {
        var json = response.Content.ReadAsStringAsync().Result;
        var jsonObject = JsonConvert.DeserializeObject<RootObject>(json);
        if (jsonObject.resourceSets.Count > 0)
        {
            if (jsonObject.resourceSets[0].resources.Count > 0)
            {
                Country = jsonObject.resourceSets[0].resources[0].address.countryRegion;
                Address = jsonObject.resourceSets[0].resources[0].address.addressLine;
            }
        }
    }

}


Call the service by passing Latitude and Longitude.

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

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);