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

Simple GPS Locator for Windows Phone 7

0.00/5 (No votes)
16 Jul 2011 1  
A simple GPS system to locate the current location of the phone

Introduction

GPS systems are going to be popular these days as many people need them on their phones, especially tourists. But, many developers do not know how to get the starting point to do GPS systems.

This is an example of a GPS system on Windows Phone 7 (the new technology from Microsoft). It shows how to get your position using the built-in locator in the phone. And then show this location on Bing Maps (like Google Maps but the Microsoft version).

Background

To work with Bing Maps, you have to get a Bing Key as a developer. It is a unique key for your application so that it is able to log in to the Bing Maps.

You can get this key from Bing Maps Portal when you register a Windows Live account.

Once you get the key, you can attach it to your application and get the maps you wish. After that, we are using an important service in the application - the Terra Service which can get the location as a text depending on latitudes and longitudes. You can add a service by Right-Clicking on the project name and choose Add Service Reference. For the Terra service, you can use this URL - http://msrmaps.com/TerraService2.asmx, and then add it to the usings with the name you named it while importing.

This is the official site: http://msrmaps.com/.

The Terra service includes some important methods to work with GPS like:

What we are interested in is the 'ConvertLonLatPtToNearestPlace' method which gets a place name from its coordinates.

There is another method (we will not use it) that does the reverse operation which is 'ConvertPlaceToLonLatPt'.

Using the Code

First, let's investigate the XAML code for the map part.

<my:Map Height="457" Name="mapBing" Width="450" CredentialsProvider="Your Key"/>

Then you have your map working, and you will notice that it will start working even in the design view before you deploy and run. Now, to the code:

Terra.TerraServiceSoapClient client = new Terra.TerraServiceSoapClient();

This part is creating an object from the service we downloaded.

public MainPage()
        {
            InitializeComponent();
            client.ConvertLonLatPtToNearestPlaceCompleted += |
                new EventHandler<ConvertLonLatPtToNearestPlaceCompletedEventArgs>(
                client_ConvertLonLatPtToNearestPlaceCompleted);
            mapBing.ZoomBarVisibility = System.Windows.Visibility.Visible;
        }

In the constructor, we create an event handler for the ConvertLonLatPtToNearestPlaceCompleted event and this is raised when the service gets the nearest location of our coordinates, and then makes the zoom buttons visible in the map.

void client_ConvertLonLatPtToNearestPlaceCompleted(object sender, 
    ConvertLonLatPtToNearestPlaceCompletedEventArgs e)
        {
            txtResult.Text = e.Result;
            mapBing.Center = new GeoCoordinate(latitude, longitude);
            mapBing.ZoomLevel = 10;
        }

This is the event handler. It prints the result to a text block and centers the map on the new co-ordinates and then we zoom 10 times to the point on the map.

Now, let's view the button click event:

txtResult.Text = "Loading....";
GeoCoordinateWatcher myWatcher = new GeoCoordinateWatcher();
var myPosition = myWatcher.Position;

latitude = 30.01;
longitude = 31.14;

if (!myPosition.Location.IsUnknown)
{
    latitude = myPosition.Location.Latitude;
    longitude = myPosition.Location.Longitude;
}

client.ConvertLonLatPtToNearestPlaceAsync(new Terra.LonLatPt { 
    Lat = latitude, Lon = longitude });

GeoCoordinateWatcher class is a built-in class in the phone locator which we use to get the current coordinates using the Position property.

I added some default coordinates because in the Windows Phone Emulator, there is no locator unit so it will not get the coordinates correctly. I added these defaults for test only. At the end, we use the Terra service object to search for the name of this location.

Points of Interest

In my work, I added a few buttons to change the map mode between Aerial, road, street, satellite, ...... etc., but I found that it is unnecessary so I removed it to make a space on the screen.

History

This is the first version, and I will come up with more advanced features soon.

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