Geocoding With Some Help From C# and Google API

Written by:  • Edited by: Michele McDonough
Updated Feb 12, 2010
• Related Guides: Google | API

Learn how Google’s API can be used to geocode a specific address and return the latitude, longitude and accuracy of that locale.

There comes a time when it is important for a business to Geo Code address / customers.. and nothing is better than using Google API to accomplish this task.

Sample problem: Create a small application using C# that uses Google API to geo code a given address / location. The C# application should show user latitude, longitude and the accuracy of the lat / long.

Solution: We can use Google’s API to geo code a given address, Google API will give us back three items we need. Lat, Long and the accuracy. The API will give us back 8 for very good match and down to 2 for poor or bad match.

Implementation: I assume you know how to program in C#, therefore I’m only going to list CodeBehind version of the application.

//include below system.Net class if its not already included

using System.Net;

//you might need a google api key if you don’t already have one

// http://code.google.com/apis/maps/signup.html

//API key fro http://YourDomain.com

string strKey = "ABQIAAAA………………PUT YOUR KEY HERE

//Address

string strAddress = "1 Main St, New York, NY 10001";

//Create a path string with address and api key

string sPath = "http://maps.google.com/maps/geo?q=" + strAddress + "&output=csv&key=" + strKey;

//Using WebClient class to download the CSV

//WebClient is part of System.Net class

WebClient client = new WebClient();

//Downloading CSV with Latitute and Longitute

//.DownloadString method download CSV file from browser

string[] eResult = client.DownloadString(sPath).ToString().Split(',');

//As you can see, I’m spliting the string into array

//I’m not using element 0 as it keeps status code if address if found, //but you can if you want too.

//Once the result / response is in string array eResult, we can access //it by calling its GetValue method and pass the 0 based index.

lbl1.Text = eResult.GetValue(1).ToString();

lbl2.Text = eResult.GetValue(2).ToString();

lbl3.Text = eResult.GetValue(3).ToString();

That’s all, its simple as that!!


Comments

Showing all 7 comments
 
Greg Mar 6, 2011 12:37 PM
BODACIOUS EXAMPLE
Thanks man, simple and straightforward "just works" example.
jialong huang Jan 24, 2011 4:08 AM
RE: Geocoding With Some Help From C# and Google API
if there has Lat, Long and the accuracy,
Should we can get detailly address information?
james Aug 19, 2010 12:12 PM
This is great!
You just solved a hell of a problem for me. It reeeaaallly took the complexity out of it all. I used it in the project I am currently working on. It fits in well. good job!
bookmarked!
James
Isda Mar 30, 2010 6:34 AM
Geocode
I´ve got 10 streets with the same name, so how can I show more results for one query an than decide the right one?
Mohammad Mar 19, 2010 10:27 AM
Blackberry
Sorry Nirav, I do not know how to create apps in Blackberry or iPhone.
neeli Mar 8, 2010 3:46 AM
Thanks
I googled a lot but unable to find a soluntion untill i found this.
great work
it helped me
thanks a lot.
Nirav Dec 4, 2009 3:56 AM
Blackberry
I have create application for Blackberry Mobile using C#.Net.
please tell me What tool i need?
how can store Database?
How can Create Application?
 
blog comments powered by Disqus
Email to a friend