Click here to Skip to main content
Click here to Skip to main content

Simple GPS Locator for Windows Phone 7

, 16 Jul 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ahmedn1
CEO Perfect Codes
Egypt Egypt
Educated at Computer Science & Information College, Mansoura University, Egypt
Set up a software development company "Perfect Code" which was in the name "Ahmedn1" or "AN1Soft"
 
Working with:
C#, VB.NET, ASP.NET, PHP, Windows Phone applications, Java, Python
 
My quote in life is
To Be Or Not To Be
Follow on   Twitter

Comments and Discussions

 
QuestionUnavailable PinmemberMember 1049321412-Jan-14 11:05 
AnswerRe: Unavailable PinmemberAhmedn117-Feb-14 10:57 
Questionmy vote of 5 PinprofessionalMesa199330-Oct-13 21:18 
QuestionBest GPS based system example souce code Pinmemberkasun61810-Oct-13 23:40 
QuestionGPS Question PinmemberLaserson8-Mar-13 9:43 
AnswerRe: GPS Question PinmemberOludayo Alli4-May-13 10:02 
GeneralMy vote of 5 Pinmemberibrahim_ragab5-Dec-12 6:32 
GeneralMy vote of 5 PinmvpKanasz Robert21-Sep-12 2:22 
GeneralMy vote of 5 PinmemberDionito23-Nov-11 15:48 
QuestionCool Article PinmemberVuyiswa Maseko18-Jul-11 15:14 
AnswerRe: Cool Article PinmemberAhmedn119-Jul-11 3:32 
GeneralRe: Cool Article PinmemberVuyiswa Maseko19-Jul-11 3:37 
GeneralRe: Cool Article PinmemberAhmedn119-Jul-11 5:38 
GeneralRe: Cool Article PinmemberVuyiswa Maseko19-Jul-11 7:01 
GeneralRe: Cool Article PinmemberMember 661742119-May-12 5:16 
Generalnice article Pinmemberkacime_198118-Jul-11 5:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 16 Jul 2011
Article Copyright 2011 by Ahmedn1
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid