My client had a "Entity Locator" on their site and needed additional data imported into the database. The data they gave me had the Entities' addresses but no Latitude and Longitude, which were essential for the program to work. In the past, I've had to use purchased data that gave zip code lat and long, but no more! With Google Maps API, I was not only able to get a more accurate Lat and Long (based on the full address, not just the zip code), but it was free!
First of all, you need to sign up for a developer key: http://code.google.com/apis/maps/signup.html
Here is a function that I wrote to get the Latitude and Longitude for a given address using the Google API. It's quite simple. You build the URL along with the query string parameters, make a request to Google's API, when the response comes back you process the results.
The parameters that you send to Google's API are:
key - the App key you got from Google when you signed up for the service.
output - the format of your results. For ease and size, I choose cvs.
q - the address you are looking for.
using System.Net;
private void GetLatAndLongGoogle(string address, string city, string state,
string zip, ref string inLat, ref string inLong)
{
/*
* Pass in the paramters: address, city, state, zip
*
* The Longitude and Latitude will be passed back out through: inLat, inLong
* (don't forget to pass these last two as "ref")
*/
/* appID is the API Key you get from Google */
string appID = "YOUrKraZYKey1w";
/* Here we build the URL with the query string parameters*/
StringBuilder url = new StringBuilder("http://maps.google.com/maps/geo?");
url.Append("output=csv&key=");
url.Append(appID);
url.Append("&q=");
url.Append(CleanForURL(address));
url.Append(",");
url.Append(CleanForURL(city));
url.Append(",");
url.Append(CleanForURL(state));
url.Append(",");
url.Append(CleanForURL(zip));
/*Create HttpWebRequest object*/
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url.ToString());
myReq.Credentials = CredentialCache.DefaultCredentials;
try
{
HttpWebResponse response = (HttpWebResponse)myReq.GetResponse();
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
string httpString = readStream.ReadToEnd();
if (httpString.StartsWith(@"200,"))
{
string[] AddressArray = httpString.Split(',');
inLat = AddressArray[2];
inLong = AddressArray[3];
}
else
{
inLat = "0.0";
inLong = "0.0";
}
}
catch (Exception e)
{
inLat = "0.0";
inLong = "0.0";
}
}
private string CleanForURL(string inStr)
{
inStr = inStr.Replace(@"&", "");
inStr = inStr.Replace(@"/", "");
inStr = inStr.Replace(@"\", "");
inStr = inStr.Replace(@"?", "");
return inStr;
}
Here is a sample of how you would call the function:
int myAddressKey = 1234;
string myAddress = "401 Main St";
string myCity = "Huntington Beach";
string myState = "CA";
string myZip = "92648";
string myLatitude = "";
string myLongitude = "";
GetLatAndLongGoogle(myAddress, myCity, myState, myZip,
ref myLatitude, ref myLongitude);
MyProcedureThatUpdatesDBWithLatAndLong(myAddressKey, myLatitude, myLongitude);
No comments:
Post a Comment