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 provides a few different free web services including geocoding on street address, ZIP code, or city and state. 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 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 uses the 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="" 
<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 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

16 thoughts on “How to convert a street address to longitude and latitude (geocoding) via web services

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


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

  3. @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.

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

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

Leave a Reply