Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Location Aware Phone Book

0.00/5 (No votes)
18 Aug 2007 2  
An article on using the Microsoft Live Search API to make a location aware phone book

Introduction

Microsoft made available to the public an API for tapping into Windows Live Search. The API allows for searching for web, news, and other types of information. For this project I am concentrating on the phone search for making a location aware phone book on my Windows Mobile communication devices.

Hardware Requirements

There are two pieces of hardware that are needed to take advantage of this program. One is a Windows Mobile Device running at least the Windows Mobile 2003 operating system and a GPS receiver. Windows Mobile 5 devices will work but you may get a warning about the code being unsigned. You can also use one of the Windows Mobile emulators that is included with Visual Studio .NET but you will need to remember to map one of the virtual ports to the GPS port on your host machine and ensure that it can connect to the Internet. The GPS receiver can be built in or external, it just needs to be mapped to a COM port. The Windows Mobile device must have access to the Internet either via WiFi or a data subscription through a phone service provider.

If you have no GPS receiver the program will still work, but you will need to select a city that will be used as the reference point for all searches.

Software Requirements

I used Visual Studio .NET 2005 for developing this project. While my preference in development languages has been C languages for over 10 years the sample included with this article is written in VB.NET.

The Windows Mobile Device will also need to have the 2.0 Compact Framework installed. If this is not present, Visual Studio will install it at the time of deployment. The 2.0 Framework with SP2 will be preferable if your GPS receiver is mapped to a port above COM9.

Getting Started

For developing against the Microsoft Live Search web service, you will need to request an Application ID for your project. Getting one is fast, easy, and free. Just visit this website to request an ID.

Creating the Project

For this project I began by creating a new "Smart Device" project targeting "Pocket PC 2003" devices. This will be a "Device Application" project. Since this project relies on the Microsoft Live Search web service the next step is to add a reference to that web service. Once the web service is added, performing a search is only a matter of building a search request object and submitting it.

Building the Search Request

The Live Search API will let you search for multiple data sources in a single request. Each data source would be described by a "SourceRequest" object. Although a search request can contain several SourceRequest objects we are only searching for phone numbers, so our searches will always have a single SourceRequest object.

This is how I've created the SearchRequest and SourceRequest objects and then associated the Source:

Dim searchRequest As SearchRequest = New SearchRequest
Dim sourceRequest() As SourceRequest = {New SourceRequest()}
searchRequest.Requests = sourceRequest

There are some general settings that must be set for the search. This includes your application ID, the language/culture for your search, the number of results that you would like to get back, the type of search, and the fields you want populated in the search results.

searchRequest.AppID = "__YOUR_APP_ID_GOES_HERE__"         
searchRequest.CultureInfo = "en-US"
sourceRequest(0).Count = 50
sourceRequest(0).Source = SourceType.PhoneBook
sourceRequest(0).SortBy = SortByType.Distance
sourceRequest(0).ResultFields = ResultFieldMask.Phone Or _
 ResultFieldMask.Address Or ResultFieldMask.Location Or ResultFieldMask.Title

Of course you will need to place your own Application ID in this code. All that is left is to specify what we are searching for and the location around which we would like to search:

searchRequest.Location = currentLocation
searchRequest.Query = txtName.Text

The "currentLocation" object here contains a location (latitude, longitude, and radius). I'll comment on this field later. The txtName.Text is a reference to a text field in which the user has entered what he or she is searching for.

Submitting the search is easy. Create a reference to the MSNSearchService object, call its search method with the search request, and save the response. The individual search results are in a collection named Responses. The fields of the objects in this collection are self explanatory (Phone, Address, so on…)

Dim searchService As MSNSearchService = New MSNSearchService()
Dim searchResponse As SearchResponse
searchResponse = searchService.Search(searchRequest)

Reading the GPS Device

NMEA GPS devices traditionally communicate using an RS232 interface (serial port). As such you will be reading from your GPS device by using the SerialPort object. Such devices output a lot of interesting data, including the UTC time, one's velocity, and other things. We only need to know our position. I've implemented a minimalistic GPS reading class. It reads each line output from the GPS receiver, looks for a message containing the longitude and latitude, and extracts the information using the following regular expression:

Dim gpsExpression As Regex = New Regex( _
       "\$GPRMC,\d*(\.\d*)?,\w,(?<LatitudeDegrees>\d+)_
    (?<LatitudeMinutes>(\d{2}\.\d+)?),(?<latDir>N|S)," + _
       "(?<LongitudeDegrees>\d+)(?<LongitudeMinutes>(\d{2}\.\d*)?),_
    (?<longDir>E|W)")

The class has it's own thread. Once instantiated it begins continually reading the current position from the GPS receiver and passes the new coordinates via an event. Special care has to be taken with this object. Since it has it's own thread it can prevent the program from unloading from memory if the main form is closed but this thread is not stopped. Calling the objects Dispose method will cause the thread to be aborted.

When attempting to detect a GPS port I enumerate the available comports and attempt to read from each one until one produces a string that looks like a GPS string (once again using a regular expression). If the GPS devices fails (which happened during my testing when the external GPS receiver's battery went dead) the GpsReader's thread will terminate.

Calculating the Distance To the Result Position

The search results contain the geographical coordinates of the result, but not the distance to the result. The distance is calculated in the form code. The equation is basically calculating the length of a great circle arc on a sphere. The function will calculate the distance in kilometers or miles (though my call to the function is only requesting miles). If you would like to use some other unit for distance you only need to know the earth's radius in that unit and set that value to the "earthRadius" variable.

What's Next?

I simplified this program for this write-up but I've already started on the next set of functionality that I would like for it to have. The GPS reader class as implemented here will be no more and will be replaced with a plug-in system allowing different types of locator services to be used (such as Assisted GPS) or positioning information from some other service). I would like for the user's search query to be saved so that frequent searches can be selected from a drop-down list. Eventually I'd like to be able to display the results on a map and possibly present driving directions. And while this may end up becoming part of another project I would also like for associates running this program to be able to use it to find each other.

Conclusion

The Microsoft Live Search API is easy to use and can be used to quickly make applications that need to take advantage of searching for information on the internet. While I've only used the program for performing a generic phone book searching the API can also restrict searches for commercial or residential entities. And of course the API can be used to search for other things such as images, news, and spell correction. Microsoft has a page available that allows anyone to sample the different types of searches that can be performed.

History

  • 5 May 2007 - Article submitted
  • 18 August 2007 - Added deployment project and work-around or COMport enumeration issue in Windows Mobile

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here