Over the last two posts I’ve talked about Microsoft pubCenter ads into your Windows Phone 7 application and Accelerometer Sensor for Windows Phone 7 all as part of a very long series I’m planning to write on Windows Phone 7 in order to help you write cool and useful applications for Windows Phone 7.
In this Post I’ll talked about Location Service and Bing Maps.
Location Service – I’ll show how to receives data from the Microsoft Location Service and displays the geographic coordinates of the device, and then show your position on the Bing Maps.
Bind Maps - demonstrates how to use the basic functionality of the Bing Maps Silverlight Control for Windows Phone and manipulate the Map Coordinate based on Geo Position.

Now before we start you need to create a Bing Maps Account, why? because if you don’t have a key you will see an annoying message on your map – like that:

Download Demo Project
Step 1: Create Bing Maps Account
Enter Bing Maps Site - https://www.bingmapsportal.com, sign in with your Live Id or create new account.

Then enter “Create or view Keys” and create new key for your application, save this key because we’ll use it latter.

Step 2: Setup your Xaml
For our application we need Map object and several textbox and buttons to control the map zoom and mode.
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.RowDefinitions>
<RowDefinition Height="36" />
<RowDefinition Height="448*" />
<RowDefinition Height="123" />
</Grid.RowDefinitions>
<my:Map Name="map" Grid.Row="1" CredentialsProvider="Your Key"/>
<TextBlock Height="30" HorizontalAlignment="Left" Margin="6,6,0,0"
Text="Latitude:" VerticalAlignment="Top" />
<TextBlock Height="30" HorizontalAlignment="Left" Margin="88,6,0,0"
Name="txtLatitude" Text="0.0" VerticalAlignment="Top" />
<TextBlock Height="30" HorizontalAlignment="Left" Margin="206,6,0,0"
Text="Longitude:" VerticalAlignment="Top" />
<TextBlock Height="30" HorizontalAlignment="Left" Margin="306,6,0,0"
Name="txtLongitude" Text="0.0" VerticalAlignment="Top" />
<TextBlock Grid.Row="2" Height="30" Margin="12,78,6,0"
Name="txtStatus" Text="N/A" VerticalAlignment="Top"
TextAlignment="Center" />
<Button Content="High Accuracy" Grid.Row="2" Height="72"
HorizontalAlignment="Right" Name="btnAccuracy"
VerticalAlignment="Top" Width="221" Click="BtnAccuracyClick" />
<Button Content="Road Mode" Height="72" HorizontalAlignment="Left"
Name="btnMapMode" VerticalAlignment="Top" Width="216"
Grid.Row="2" Click="BtnMapModeClick" />
</Grid>
Step 3: Reinitialize the GeoCoordinateWatcher
The GeoCoordinateWatcher Supplies location data that is based on latitude and longitude coordinates.
private GeoCoordinateWatcher _geo;
public GeoPositionAccuracy Accuracy = GeoPositionAccuracy.High;
private string _mode = "Road";
private void PhoneApplicationPageLoaded(object sender, RoutedEventArgs e)
{
LocationInitialize();
}
private void LocationInitialize()
{
// Reinitialize the GeoCoordinateWatcher
_geo = new GeoCoordinateWatcher(Accuracy) { MovementThreshold = 20 };
// Add event handlers for StatusChanged and PositionChanged events
_geo.StatusChanged -= GeoStatusChanged;
_geo.PositionChanged -= GeoPositionChanged;
_geo.StatusChanged += new EventHandler
<GeoPositionStatusChangedEventArgs>(GeoStatusChanged);
_geo.PositionChanged += new EventHandler<GeoPositionChangedEventArgs
<GeoCoordinate>>(GeoPositionChanged);
// Start data acquisition
_geo.Start();
}
Step 4: Define Control Buttons
After we add four, two buttons for accuracy and map mode and two for zoom actions and we need to define the code behind:
The accuracy check what the current accuracy on the GeoCoordinateWatcher, replace it and reinitialize the watcher.
The Map Mode does the same but without reinitialize the watcher because we are changing the Map object.
The Zoom In and Zoom out increase or decrease the zoom on the map.
private void BtnAccuracyClick(object sender, RoutedEventArgs e)
{
Accuracy = Accuracy == GeoPositionAccuracy.Default ?
GeoPositionAccuracy.High : GeoPositionAccuracy.Default;
btnAccuracy.Content = Accuracy == GeoPositionAccuracy.High ?
"Default Accuracy" : "High Accuracy";
LocationInitialize();
}
private void BtnMapModeClick(object sender, RoutedEventArgs e)
{
if (_mode == "Road")
{
map.Mode = new AerialMode();
_mode = "Aerial";
btnMapMode.Content = txtStatus.Text = "Aerial Mode";
}
else
{
map.Mode = new RoadMode();
_mode = "Road";
btnMapMode.Content = txtStatus.Text = "Road Mode";
}
}
private void ZoomInClick(object sender, EventArgs e)
{
var zoom = map.ZoomLevel;
map.ZoomLevel = ++zoom;
}
private void ZoomOutClick(object sender, EventArgs e)
{
var zoom = map.ZoomLevel;
map.ZoomLevel = --zoom;
}
Step 5: GeoPositionChanged and GeoStatusChanged
Now to the fun part, the GeoCoordinateWatcher has two events:
- GeoStatusChanged - Indicates that the status of the GeoCoordinateWatcher object has changed.
The Status Changed can tell us is the device is not support Location or we change some properties of the GeoCoordinateWatcher.
- GeoPositionChanged - Indicates that the latitude or longitude of the location data has changed.
The position changed event will tell us what is the current Latitude and Longitude of the device. using those values we can define GeoCoordinate object and define the Map center to those coordinates, this way the user will see what is his location.
void GeoPositionChanged(object sender,
GeoPositionChangedEventArgs<GeoCoordinate> e)
{
// Update the TextBlocks to show the current location
txtLatitude.Text = e.Position.Location.Latitude.ToString("0.000");
txtLongitude.Text = e.Position.Location.Longitude.ToString("0.000");
var cor = new GeoCoordinate(e.Position.Location.Latitude,
e.Position.Location.Longitude);
//Using the current coordinates define the Map center
//to those coordinates.
map.Center = cor;
map.ZoomLevel = 10;
}
void GeoStatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
this.Dispatcher.BeginInvoke(() =>
{
switch (e.Status)
{
case GeoPositionStatus.Disabled:
// The location service is disabled or unsupported.
// Alert the user
txtStatus.Text = "location is unsupported on this device";
break;
case GeoPositionStatus.Initializing:
// The location service is initializing.
// Disable the Start Location button
txtStatus.Text = "initializing location service,"
+ Accuracy;
break;
case GeoPositionStatus.NoData:
// The location service is working, but it cannot get
// location data Alert the user and enable the Stop
//Location button
txtStatus.Text = "data unavailable," + Accuracy;
break;
case GeoPositionStatus.Ready:
// The location service is working and is receiving
//location data Show the current position and enable the
// Stop Location button
txtStatus.Text = "receiving data, " + Accuracy;
break;
}
});
}
Download Demo Project
