Global Localization in Windows Phone

Introduction

The Global Positioning System (GPS) is one of the revolutionary concepts in SmartPhones. One of the most used apps is online maps. Whether it is Google Maps or Bing Maps, many things can be done with maps. Many of our apps are based on localization, and all of them use this GPS concept in their backend.

Global Positioning System

Background

In this demonstration, we have two parts. In the first part we determine the actual position of the device (in other words the exact Longitude and Latitude). Then, implement that information in a Bing Maps Control.

Isn’t it cool enough? ;)

Procedure

Step 1: Start your project and create a look alike UI as in the following:

Start your Project

Here, I have used a Text Box, Text Block, Button and Rectangle to provide a smart look. Later on, we will use a Bing Maps Control on a Big Rectangle Frame.

Step 2: Further, add your "System.Device" reference to your project.

System Device reference

Next, you need to add a "using System.Device.Location;" to your project.

Step 3: Now, create a reference for "GeoCordinateWatcher GeoCoordinateWatcher geo;" (reference only);

Step 4: Since we want GPS yo start itself, we will keep our code inside the Constructor as in the following:

geo = new GeoCoordinateWatcher();

try

{

    geo.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(geo_PositionChanged);

    geo.Start();

}

catch

{

    MessageBox.Show("ERROR");

}

Here, the app asked the event to determine when the Position got changed. It is done by a PositionChanged event.

Step 5: Now we need to handle that event handler geo_PositionChanged as in the following:

LatitudeTextBox.Text = e.Position.Location.Latitude.ToString();
LongitudeTextBox.Text = e.Position.Location.Longitude.ToString();

Here, LatitudeTextBox and LongitudeTextBox are the two TextBoxes for my project.

Step 6: We now have the Latitude and Longitude of the device.

And it will be great if we use this information in a Map Control. So, add a Bing Map Control inside that Rectangle (let it be map1). And, use a Button (let it be TRACK) to raise the event for showing the Bing map.
 

private void Track_Click(object sender, RoutedEventArgs e)

{

     map1.Visibility = Visibility.Visible;

     map1.Center = new GeoCoordinate(geo.Position.Location.Latitude, geo.Position.Location.Longitude);
 }


There is nothing complicated in the GeoCordinate() method. In that method we are ing two arguments, Latitude and Longitude, to point in the MAP as the center.

MAP

Conclusion

Here we have a small app that shows a use of GPS and Bing Map together. You can use their various methods and fields to create an innovative app. If you have done that and encounter complications with execution, you can try our attached source code.


Similar Articles