Address Validation/Normalization Using Virtual Earth
So everyone loves the snazzy demo of Virtual Earth where they in a few lines of HTML and JavaScript draw a map (http://dev.live.com/virtualearth/sdk/). But what about doing some hard stuff, what if you want to "validate" the addresses in your address book to ensure that they are correct. Well Virtual Earth can help you here also. They have a suite of web services (http://msdn2.microsoft.com/en-us/library/aa286513.aspx) that you can do all kinds of stuff with. In my case I want to throw it a string that could be an address and get what it thinks the most likely "real" address is for that string.
Lets say for example I had the address "400 orange orlando, fl 32810" as you can see I don't know what type of street it is (RD, LN, etc.) and I also don't have the extended zip code. Well with a few lines of code I can pass that address to Virtual Earth and they will give me back a list of possible choices.
FYI at the time of writing this post they are re-branding this service, changing its name from MapPoint Web Service to Virtual Earth so you might need to use the old name if searching for more information on this topic.
Step 1: go to https://mappoint-css.live.com/CSCV3 and sign up for a developer account. This will take a while since they need to provision your account.
Step 2: go to the verify credentials screen in the VE admin console where you created your account and test your password to ensure that everything has been setup and is working. (DONT FORGET THIS STEP!!!)
Step 3: using the WSDL URL from that screen create a web service reference in your .NET application
Step 4: Write the code! Here you can see that I new up a instance of my service and pass it some credentials. Then I create an address object with the address that I want to "validate". I then put that address object into an address specification that tell VE to look in North America for that address. Finally I call the service to get the results and iterate over them printing them to the console.
FindServiceSoap findService = new FindServiceSoap();
findService.Credentials = new System.Net.NetworkCredential("Web Service Account ID", "Password");
Address myAddress = new Address();
myAddress.FormattedAddress = "400 orange orlando, fl 32810";
FindAddressSpecification findAddressSpec = new FindAddressSpecification();
findAddressSpec.InputAddress = myAddress;
findAddressSpec.DataSourceName = "MapPoint.NA";
FindResults foundAddressResults = findService.FindAddress(findAddressSpec);
foreach (FindResult fr in foundAddressResults.Results)
{
Console.WriteLine(fr.FoundLocation.Address.FormattedAddress);
}