Click here to Skip to main content
11,706,455 members (32,532 online)
Click here to Skip to main content

Simple GPS Locator for Windows Phone 7

, 16 Jul 2011 CPOL 52K 10.6K 30
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

You may also be interested in...

Comments and Discussions

 
QuestionUnavailable Pin
Member 1049321412-Jan-14 10:05
memberMember 1049321412-Jan-14 10:05 
AnswerRe: Unavailable Pin
Ahmedn117-Feb-14 9:57
memberAhmedn117-Feb-14 9:57 
Questionmy vote of 5 Pin
Mesa199330-Oct-13 20:18
professionalMesa199330-Oct-13 20:18 
QuestionBest GPS based system example souce code Pin
kasun61810-Oct-13 22:40
memberkasun61810-Oct-13 22:40 
QuestionGPS Question Pin
Laserson8-Mar-13 8:43
memberLaserson8-Mar-13 8:43 
AnswerRe: GPS Question Pin
Oludayo Alli4-May-13 9:02
memberOludayo Alli4-May-13 9:02 
GeneralMy vote of 5 Pin
ibrahim_ragab5-Dec-12 5:32
memberibrahim_ragab5-Dec-12 5:32 
GeneralMy vote of 5 Pin
Kanasz Robert21-Sep-12 1:22
mvpKanasz Robert21-Sep-12 1:22 
GeneralMy vote of 5 Pin
Dionito23-Nov-11 14:48
memberDionito23-Nov-11 14:48 
QuestionCool Article Pin
Vuyiswa Maseko18-Jul-11 14:14
memberVuyiswa Maseko18-Jul-11 14:14 
AnswerRe: Cool Article Pin
Ahmedn119-Jul-11 2:32
memberAhmedn119-Jul-11 2:32 
GeneralRe: Cool Article Pin
Vuyiswa Maseko19-Jul-11 2:37
memberVuyiswa Maseko19-Jul-11 2:37 
GeneralRe: Cool Article Pin
Ahmedn119-Jul-11 4:38
memberAhmedn119-Jul-11 4:38 
GeneralRe: Cool Article Pin
Vuyiswa Maseko19-Jul-11 6:01
memberVuyiswa Maseko19-Jul-11 6:01 
GeneralRe: Cool Article Pin
Member 661742119-May-12 4:16
memberMember 661742119-May-12 4:16 
Generalnice article Pin
kacime_198118-Jul-11 4:02
memberkacime_198118-Jul-11 4: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
Web04 | 2.8.150819.1 | Last Updated 16 Jul 2011
Article Copyright 2011 by Ahmedn1
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid