Several years ago, I had a school project that required converting some addresses to longitude/latitude coordinates. We then took the coordinates and placed them on a street map for a mobile device application. The addresses came from a “Yellow Pages” like system lookup service which provided a phone number, street address, name, and business category. But the Yellow Pages service didn’t provide a geographic coordinate.
First, I tracked down a free and very useful web service that provides this functionality. The web site GeoCoder.us provides a few different free web services including geocoding on street address, ZIP code, or city and state. GeoCoder.us offers SOAP, XML-RPC, REST-ful RDF, and REST-ful CSV formats.
We’ll use the web service the requires a street address, so we can get a more accurate coordinate.
string street = "1060 west addison street";
string city = "chicago";
string state = "il";
The GeoCoder.us URL requires a specific parameter format. Here we append the street, city, and state to the base URL. Then we create and load the URL into an XML document. By passing the URL to the XmlDocument Load method, it will handle calling and reading the web response.
string geocoderUri = string.Format(
street, city, state);
XmlDocument geocoderXmlDoc = new XmlDocument();
Next, we need to add a namespace manager since GeoCoder.us uses the W3.org Basic Geo (WGS84 lat/long) vocabulary. Below is an example of this vocabulary and the actual response that was loaded into our XmlDocument.
<dc:description>1060 W Addison St, Chicago IL 60613</dc:description>
The namespace manager is needed so that we can select the long and lat XML nodes. Below we select the two XML nodes and parse them.
XmlNamespaceManager nsMgr =
string sLong = geocoderXmlDoc.DocumentElement.SelectSingleNode(
string sLat = geocoderXmlDoc.DocumentElement.SelectSingleNode(
double latitude = Double.Parse(sLat);
double longitude = Double.Parse(sLong);
Console.WriteLine("Lat: " + latitude + " Lon: " + longitude);
Finally, the coordinates are printed to the console and appear as:
Lat: 41.947372 Lon: -87.655788
Update: 10/6/2007 Once you have the coordinates, you can perform radius searching. For example, you can query for all records within 5 miles of each other or find the nearest record to another record. Troy DeMonbreun provides an example of how to calculate the distance between two geographic coordinates using a SQL Server UDF (User Defined Function). The GeoCoder.us blog also discusses how to calculate distances using two geographic coordinates.
Update 2: 10/19/2007 Yahoo also provides a very nice Web Service for geocoding at Yahoo! Maps Web Services – GeoCoding API. Yahoo uses the same REST method for the accessing the Web Service, but with separate URL parameters for city, address, and state. Yahoo’s XML response doesn’t use namespaces and won’t require the use of a NamespaceManager. You’ll be able to just use SelectSingleNode(@”//Longitude”) and SelectSingleNode(@”//Latitude”) to collect the geographic coordinates.