GeoCode data retrieve(Latitude, Longitude) for Maps application (C# with third party APIs)

Recently we had a project which was related to show items on google map embedded inside CRM interface(MS CRM) based on the address of the items, e.g. show nearest accounts to my current location and some other details. We had the address of the items from a source system.

Challenges faced :

1. embedd google map to CRM interface

2. get latitude, longitude of the items based on address

3. fetch and display the items based on the geocode data

On the top of it, this was needed to support mobility(CRM Mobile apps) where it should support both Online and Offline(after synchronising to local device and working without network connection). This was taken care of a third party apps.

In short, our main aim is to get longitude, latitude based on address fields. We tried 3 different ways, google api, bing api and mapquest api. These companies provide an api which can be accessed through codes(C#, JSon, JavsScript etc, both server side and client side).

Google Api:

This is pretty good, only thing a premium account is needed to make regular updates. If you have fixed budget, this is not a good option, as per google they provide approx 2500 hits per day but they have blocked us since we were doing it again and again for quite a few days 🙂 ( ). Below is the code sample to access latitude, longitude based on address available. We passed the request to the Google api and in return accepted the response in XML format(there are other response type also, say JSon).

Pre requisites:

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. Cryptographic key

2. Client Id

Once you have ordered and receive those above two keys, here is the simple piece of code to use an address to retrieve longitude and latitude values. In my case, I have used the address in this format : “Street Number” + “Street Name” + “City” + “Country”; you can provide zip code or second street name etc. also. I would say, it is preferable to replace the spaces in the address string by “+”.

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(" " + 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:


bhaskar jyoti


About Joon
quite ordinary man

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: