Jim's Blog Ramblings about novels, comics, programming, and other geek topics


How to convert a street address to longitude and latitude (geocoding) via web services

Google AdSense

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.

<rdf:RDF xmlns:dc="http://purl.org/dc/elements/1.1/" 
<geo:Point rdf:nodeID="aid62658379">
<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 =
    new XmlNamespaceManager(geocoderXmlDoc.NameTable);

string sLong = geocoderXmlDoc.DocumentElement.SelectSingleNode(
        @"//geo:long", nsMgr).InnerText;
string sLat = geocoderXmlDoc.DocumentElement.SelectSingleNode(
        @"//geo:lat", nsMgr).InnerText;

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.


kick it on DotNetKicks.com

James Welch

James Welch is a software engineer in Vermont working for a large information technology company and specializing in .NET. Additionally, he holds a Master’s Degree in Software Engineering and a Bachelor of Science Degree in Computer Science. Jim also enjoys local craft beer, comic books, and science-fiction and fantasy novels, games, and movies.

Twitter Google+ 

Comments (15) Trackbacks (1)
  1. Well, it’s actually Troy, not Tony. 😉

  2. I need something that will take a data base and convert all the addresses to coordinate I can use on my GIS application.


  3. You can loop through each database record and then perform the lat/lon lookup discussed in this article. You would just need to query your table, loop through the record set, pass in the values to the above code, get the coordinates, update the coordinate fields of the record, and save the record.

  4. provided you have less than 5000 records in your db because that’s the api call limit sets by yahoo.

  5. You could also try the USC WebGIS geocoder (https://webgis.usc.edu). It is free and offers several API’s, or you can upload a database for online batch processing.

  6. how do you convert an address into latitude and longitude? Is it possible

  7. how do you change latitude and longitude into a address?

  8. @runawaychild,

    > how do you convert an address into latitude and
    > longitude? Is it possible

    It is possible. If you read the blog entry, it describes the process.

  9. Jim,

    Great information. Very helpful! Thanks!


  10. Hi Jim,

    Is it possible to get the longitude & latitude value from IP address ??

    Please let me know !
    Thanks in advance.

    • Not specifically, but you can use a geoIP web service to do so.

      That service uses reverse DNS lookups to find out the name of the ISP’s server that the user is connected through. Then they cross-reference that server name against their database to determine the name of the city, then they grab the Latitude and Longitude of the city.

      If you do some web searches, then I’m sure you’ll find a web service that will do what you want.

    • Yes, but your code to run on a browser that supports the navigator.geolocation object. here is a real solid overview:

  11. How can I put the response of a web service into a textbox so that I can submit the textbox text into my database?

    Here is the result of my web service.

    <string xmlns="http://svxx/"97.5588900,37.6669989

    • You can just escape (aka encode) the result of your web service using UrlEncode, then place the encoded string into a multiline text box.

  12. how to get address (city etc) from gps coordinates?

Leave a Reply