Click here to Skip to main content
15,867,870 members
Articles / Mobile Apps / Windows Phone 7

Simple GPS Locator for Windows Phone 7

Rate me:
Please Sign up or sign in to vote.
4.89/5 (17 votes)
16 Jul 2011CPOL3 min read 82.8K   10.9K   30   17
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.

XML
<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:

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

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

C#
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.

C#
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:

C#
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)


Written By
Technical Lead Paymob Solutions
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

Comments and Discussions

 
Questionwhy ? Pin
bryanjaysena@yahoo.com18-Aug-16 18:56
bryanjaysena@yahoo.com18-Aug-16 18:56 
QuestionUnavailable Pin
Member 1049321412-Jan-14 10:05
Member 1049321412-Jan-14 10:05 
AnswerRe: Unavailable Pin
Ahmedn117-Feb-14 9:57
Ahmedn117-Feb-14 9:57 
Questionmy vote of 5 Pin
Mesa199330-Oct-13 20:18
professionalMesa199330-Oct-13 20:18 
QuestionGPS Question Pin
Laserson8-Mar-13 8:43
Laserson8-Mar-13 8:43 
AnswerRe: GPS Question Pin
Oludayo Alli4-May-13 9:02
Oludayo Alli4-May-13 9:02 
GeneralMy vote of 5 Pin
ibrahim_ragab5-Dec-12 5:32
professionalibrahim_ragab5-Dec-12 5:32 
GeneralMy vote of 5 Pin
Kanasz Robert21-Sep-12 1:22
professionalKanasz Robert21-Sep-12 1:22 
GeneralMy vote of 5 Pin
Dionito23-Nov-11 14:48
Dionito23-Nov-11 14:48 
QuestionCool Article Pin
Vimalsoft(Pty) Ltd18-Jul-11 14:14
professionalVimalsoft(Pty) Ltd18-Jul-11 14:14 
AnswerRe: Cool Article Pin
Ahmedn119-Jul-11 2:32
Ahmedn119-Jul-11 2:32 
GeneralRe: Cool Article Pin
Vimalsoft(Pty) Ltd19-Jul-11 2:37
professionalVimalsoft(Pty) Ltd19-Jul-11 2:37 
GeneralRe: Cool Article Pin
Ahmedn119-Jul-11 4:38
Ahmedn119-Jul-11 4:38 
GeneralRe: Cool Article Pin
Vimalsoft(Pty) Ltd19-Jul-11 6:01
professionalVimalsoft(Pty) Ltd19-Jul-11 6:01 
GeneralRe: Cool Article Pin
Member 661742119-May-12 4:16
Member 661742119-May-12 4:16 
Generalnice article Pin
kacime_198118-Jul-11 4:02
kacime_198118-Jul-11 4:02 
nice article

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

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