Share via


C#: Getting longitude, latitude using geolocation API

Here are the ways to use simple C# code in order to get longitude, latitude values based on an address and with the help of APIs.

Bing

 Link  Explanation

Find a Location by Address

Use these URL templates to get latitude and longitude coordinates for a location by specifying values such as a locality, postal code, and street address.

Find a Location by Point

Use this URL template to get the location information associated with latitude and longitude coordinates.

Find a Location by Query

Use these URL templates to get latitude and longitude coordinates that correspond to location information provided as a query string.

Location Data

Use this description to understand the results returned in the response to a Locations API request.

Bing Maps REST services

Full information on the Bing Maps Rest services is here: http://msdn.microsoft.com/en-us/library/ff701713.aspx

 

Google

Prerequisites

Based on your need, either in corporate level or personal level and number of data to be retrieved, you have to order separate types of keys from Google. Basically there are two keys that are important:

  1. 1. Cryptographic key
  2. 2. Client Id

Once you have ordered and received those above two keys, here is the simple piece of code to use an address to retrieve longitude and latitude values.

In this case, the address is in this format : "Street Number" + "Street Name" + "City" + "Country"; You can provide zip code or second street name. also. I would say, it is preferable to replace the spaces in the address string by using "+".



      XmlDocument doc =     new  XmlDocument();               
      string clientId = "your-ordered-clientid-from-Google"; //replace this with
      your client id                 
      string key = "cryptokey-ordered-from-Google"; //replace this with your cryptographic key
                 string address ="StrretNumber+Streetname,+CityName,+Country"; //replace this with your addess  
              
      var urlRequest =     "/maps/api/geocode/xml?address="  + address + "&client="  + clientId;
      System.Security.Cryptography.HMACSHA1 myhmacsha1 =     new  System.Security.Cryptography.HMACSHA1();
      myhmacsha1.Key = Convert.FromBase64String(key);  
      var hash = myhmacsha1.ComputeHash(System.Text.Encoding.ASCII.GetBytes(urlRequest));  
      string signature = Convert.ToBase64String(hash).Replace("+", "-").Replace("/", "_");
              
      WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultNetworkCredentials;  
              
      doc.Load(    "          https://maps.googleapis.com/maps/api/geocode/xml?address=        " + address + "&client=" + clientId + "&signature=" + signature);
              
      string longitude = doc.SelectSingleNode("//GeocodeResponse/result/geometry/location/lng").InnerText;
      string latitude = doc.SelectSingleNode("//GeocodeResponse/result/geometry/location/lat").InnerText;

The detailed instructions can be found on this link: https://developers.google.com/maps/documentation/business/webservices/auth