Parsing Addresses and More with the MapPoint Web Service Find APIs
(Page 1 of 5 )
In this conclusion to a five-part series on the Find Service, you will learn about parsing addresses, optimizing performance, and more. It is excerpted from chapter six of the book
Programming MapPoint in .NET, written by Chandu Thota (O'Reilly; ISBN: 0596009062). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.
Parsing Addresses
We have seen various capabilities of Find Service to find places, addresses, nearby entities, and points of interest, but what happens if you have an address in an unstructured or unformatted form? What if you want to create an application where your users can type their address in a textbox without worrying about the formatting? How do you parse the address field to understand various parts of the address? You can use the FindServiceSoap.ParseAddress method for these purposes. The ParseAddress method takes two arguments, input address as a string and an optional country/region name, and returns an Addressobject for valid addresses. For example, if you have the address 1 Microsoft Way, Redmond, WA in string format, you can use theParseAddressmethod to parse it into anAddressobject:
//Create a web service proxy
FindServiceSoap findService = new FindServiceSoap();
//Assign credentials
. . .
//Parse a string into a valid address object
Address address =
findService.ParseAddress("1 Microsoft Way, Redmond", "United States");
One of the greatest advantages of this method is that you can implement one user interface that can perform both find place and find address depending on what users input without having to design two different UIs for two different purposes.
Asynchronous Programming with Find Service
When developing applications using Web Service, keep in mind that you are making a network round-trip with every method call, which has serious implications on your application’s performance in terms of responsiveness. For example, since calls over the network take a long time to return, you don’t want to block the UI thread for your Windows application. This is where the asynchronous programming patterns come to the rescue. Using the .NET framework, it is easy to call Web Service methods asynchronously. So, let’s see how you would implement a FindServiceSoap.Find method call asynchronously.
Asynchronous Programming for Windows Applications
When you generate the MapPoint Web Service proxy class using Visual Studio .NET, it also generates the necessary methods for asynchronous programming. For example, if you look for the FindServiceSoap.Find method, you also find the FindServiceSoap.BeginFind and FindServiceSoap.EndFind methods in the proxy class. TheBeginandEndmethod pairs enable the asynchronous programming patterns for your web service client applications. Using these methods is really easy; in a synchronous scenario, yourFindcall looks like the following code:
//Call the Find Method
FindResults findresults = findsoap.Find(findspec);
//Now display find results
DisplayFindResults(findresults);
If this code is running on the UI thread, it does not get to theDisplayFindResultsmethod until theFindmethod call completes and returns thefindresultsvalue; during this period, users of your application may find it unresponsive. To avoid this situation, create a worker thread and call theFindmethod using it so that your UI thread is free during this long network round-trip. In fact, that’s exactly what theBeginFindandEndFindmethods do behind the scenes. To implement the previous code using asynchronous methods, you would do something similar to the following:
First, define a callback method for your asynchronous method calls:
private void FindServiceCallback(IAsyncResult ar)
{
FindServiceSoap findSoap
= ar.AsyncState as FindServiceSoap;
if(findSoap == null)
return;
FindResults findresults = findSoap.EndFind(ar);
DisplayFindResults(findresults);
}
Next, modify your find call to become an asynchronousBeginFindcall:
//Async call to find
AsyncCallback callback = new AsyncCallback(FindServiceCallback);
findsoap.BeginFind(findspec, callback, findsoap);
TheBeginFindinvokes theFindmethod on a different (worker) thread and passes a pointer to theFindSeviceCallbackmethod as a callback method; when theFindmethod returns aFindResultsinstance, the callback delegate is invoked so that theFindServiceCallbackmethod gets executed on the UI thread again. In theFindServiceCallbackmethod, you need to obtain theFindResultsreturned by theFindmethod by calling theEndFindmethod and displaying them. Keep in mind that the HTTP session is kept alive during this asynchronous operation behind the scenes—this pattern is asynchronous at your application thread level but not at the HTTP communication level.
Asynchronous Programming for Web Applications
Multithreaded programming works well for Windows applications if you are calling web services, but wouldn’t it be nice to adopt this asynchronous programming model for web applications as well? Wouldn’t it be convenient to develop more responsive applications without doing a complete page refresh? You can do these things with a combination of JavaScript and Msxml2.XMLHTTP ActiveX control. The use of JavaScript with asynchronous XML messaging is called Asynchronous JavaScript and XML, or simply AJAX. While AJAX terminology is fairly new to web application development, the use of JavaScript and XMLHTTP is not. In this section, I will go over some scenarios where AJAX can be used in your web applications to improve the overall user experience.
Next: AJAX-Enabling Your Web Applications >>
More BrainDump Articles
More By O'Reilly Media
|
This article is excerpted from chapter six of the book Programming MapPoint in .NET, written by Chandu Thota (O'Reilly; ISBN: 0596009062). Check it out today at your favorite bookstore. Buy this book now.
|
|