Yahoo provides their Geocoding Web Service to allow you to find specific latitude and longitude for an address; it's also useful for cleaning addresses where you're not sure of the exact zip code, spelling of the street, etc.
There are actually two web services - the orginal Yahoo Maps Web Service, and the new PlaceFinder service. The original web service has been deprecated, but that's what I'll be demonstrating today; the next post will be about the new PlaceFinder service.
The first step is to obtain a Yahoo! Developer Network ID at:
https://developer.apps.yahoo.com/wsregapp/
All of the information on the request parameters and response fields is located at:
http://developer.yahoo.com/maps/rest/V1/geocode.html
You'll send the street, city, state and zip to the API in this format:
http://local.yahooapis.com/MapsService/V1/geocode?appid=YD-9G7bey8_JXxQP6rxl.fBFGgCdNjoDMACQA--&street=701+First+Ave&city=Sunnyvale&state=CA&zip=94089
(Note, the appid above is Yahoo's demo ID, you can't use it in your code...)
...and get a response back like:
<ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yahoo:maps" xsi:schemaLocation="urn:yahoo:maps http://api.local.yahoo.com/MapsService/V1/GeocodeResponse.xsd">
- <Result precision="address">
<Latitude>32.799670</Latitude>
<Longitude>-96.820005</Longitude>
<Address>1950 N Stemmons Fwy</Address>
</Result>
</ResultSet>
It's incredibly simple to use this via code - just pass the Yahoo Url to an XmlTextReader and parse out the Xml response, like this:
public struct Address
{
public string Street;
public string City;
public string State;
public string Zip;
public string Country;
public string Lat;
public string Long;
public string Precision;
}
public Address CodeAddresses(string street, string city, string state, string zip)
{
string yahooURL = "http://local.yahooapis.com/MapsService/V1/geocode?appid=<your app Id>&street=" + street + "&city=" + city + "&state=" + state + "&zip=" + zip;
Address SearchAddress = new Address();
XmlTextReader reader = new XmlTextReader(yahooURL);
reader.WhitespaceHandling = WhitespaceHandling.Significant;
while (reader.Read())
{
switch (reader.Name.ToString())
{
case "Result":
if (reader.NodeType == XmlNodeType.Element)
SearchAddress.Precision = reader.GetAttribute("precision");
break;
case "Address":
SearchAddress.Street = reader.ReadString().ToString();
break;
case "City":
SearchAddress.City = reader.ReadString().ToString();
break;
case "State":
SearchAddress.State = reader.ReadString().ToString();
break;
case "Zip":
SearchAddress.Zip = reader.ReadString().ToString();
break;
case "Country":
SearchAddress.Country = reader.ReadString().ToString();
break;
case "Latitude":
SearchAddress.Lat = reader.ReadString().ToString();
break;
case "Longitude":
SearchAddress.Long = reader.ReadString().ToString();
break;
}
}
return SearchAddress;
}
In future posts I'll go over the PlaceFinder service as well as Google's geocoding API.
Technorati Tags: Mapping