views:

257

answers:

2

How to convert a city/state to a geo location (lat/long) using bing?

+2  A: 

This example uses Yahoo's API but gets the job done for me. Will work if you just submit a zipcode or city,state. I use a dataset because I'm addicted to them.

private void YahooParseLatLon(string address_)
{
HttpWebRequest feed =
HttpWebRequest.Create("http://local.yahooapis.com/MapsService/V1/geocode?
appid=YOUR_APID&street=" + address_) as HttpWebRequest;

WebResponse feedresponse = default(WebResponse);
feedresponse = feed.GetResponse();
DataSet data = new DataSet();
data.ReadXml(feedresponse.GetResponseStream());
if (!string.IsNullOrEmpty(data.Tables(0).Rows(0).Item("Address"))) {
//process lat lon values
string lat = data.Tables(0).Rows(0).Item("Latitude");
string lon = data.Tables(0).Rows(0).Item("Longitude");
}
else {
//process no address found
}
data.Dispose();
feedresponse = null;
feed = null;
}

I use this to collect lat/lon for addresses on new orders placed in my company's system. I then use this data to populate a map integrated into a scheduling application for our field agents.

pcpimpster