Advertisement
Tech

Learn to Geocode Address with C# and 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.

By Mohammad
Desk Tech
Reading time 2 min read
Word count 305
Web development Internet Advanced tutorials
Learn to Geocode Address with C# and Google API
Advertisement
Quick Take

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

On this page

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.

Advertisement

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.

Advertisement

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

using System.Net;

Advertisement

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

//https://developers.google.com/maps/location-based-apps

Advertisement

//API key for https://YourDomain.com

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

Advertisement

//Address

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

Advertisement

//Create a path string with address and api key

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

Advertisement

//Using WebClient class to download the CSV

//WebClient is part of System.Net class

Advertisement

WebClient client = new WebClient();

//Downloading CSV with Latitute and Longitute

Advertisement

//.DownloadString method download CSV file from browser

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

Advertisement

//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.

Advertisement

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

Advertisement

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

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

Advertisement

That’s all, its simple as that!!

Keep Exploring

More from Tech

Filed under
Web development Internet
More topics
Advanced tutorials
Advertisement