Webinar SignUp

Today’s Integration: Zip Code Look Up

The following post is a plaudit to Jim Layton, Developer-Extraordinaire. Jim spends his working days in deep, deep code and likes to shake it up occasionally with a little light fun. Here’s something quick and easy – how to integrate an online service in just a few short minutes.

“Over the past few years, Yahoo has been creating many useful and fun tools to help web-developers develop ‘in the cloud’.  One of the most useful services for online shopping cart is Yahoo’s GeoCode service, which has a variety of tools to look-up address information around the world.  The best part of yahoo’s tools: they’re free to use.

To get started using Yahoo’s ‘Where’ API, go to http://www.yahooapis.com/geo/placefinder/ – there is lots of information on how to sign up to use the service.  Once you are signed up, you are good to go to start using it.

So, suppose we wanted to know what City and State the zip code 97520 belonged to?  That’s easy, simply browse to this URL: http://where.yahooapis.com/geocode?postal=97520&country=US – you can see the XML returned with an Ashland, OR in the <city> and <statecode> elements.

One way we can leverage this cool service it to integrate it into the AspDotNetStorefront Create Account page, so that when customers enter their zip-code it automatically fills in their city and state.  It’s actually pretty easy to do.  For the purposes of this article, I’m going to give you a few steps to get you started.  The first step is to create a class that calls out to the yahoo GeoCode service.  Here is a code snippet to get you started:

public class ZipServiceUtility
{
public static CityState GetCityByZip(String country, String zipCode)
{
string appId = “myappId”;
string yahooUrl = string.Format(“http://where.yahooapis.com/geocode?postal={0}&country={1}&appid={2}”, zipCode, country, appId);
XmlTextReader reader = new XmlTextReader(yahooUrl);

// Parse out the city and state here

}
}

The XmlTextReader will get filled up with the XML you saw above – you’ll then need to parse out the City and State code and store it somewhere for later use.

Next, you’ll want to add an “OnTextChanged” to the BillingZip TextBox on the createaccount.aspx page:

<asp:TextBox ID=”BillingZip” Columns=”14″ MaxLength=”10″ runat=”server” CausesValidation=”true” ValidationGroup=”createaccount” OnTextChanged=”BillingZip_TextChanged” AutoPostBack=”true” />

In the code-behind (createaccount.aspx.cs) – popuplate the event  BillingZip_TextChanged with code that calls the Yahoo API, then set the city text box and and state drop-down with the data you get from the service.  That in a nut-shell is how to utilize one of the vast arrays of new cloud based services to improve your shoppers’ experience in AspDotNetStorefront.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Posted in Integrations | Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *


+ 7 = 13

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>