Automating the Windows Map App

Windows allows a Windows Store application to register as the default handler for a certain URI schema. This process is called Protocol Activation and it can be leveraged in the WinRT as well as the WinJS framework.

The Windows Map app is registered for protocol activation and the URI schema is documented here. Through protocol activation you can control the map view, find places or business listings, calculate routes or even map entire collections of points of interest.

 ProtocolActivation

In this quick example, we use this capability to display the location where we captured a photo. Rather than starting from the scratch, we build upon the Simple Image Sample from the Windows Dev Center. The first scenario in this application reads and writes properties of an image – including the coordinates (latitude and longitude) where we captured the photo.

SimpleImagingSample

There are a few changes that we need to apply to this app in order to launch the Map app and center on this location.

In the application markup language of scenario 1 we do the following: 

  1. Replace the boxes for degrees, minutes and seconds as well as the one for the compass orientation with a single text-box in which we will display the latitude and longitude in decimal degrees.
  2. Add a button that will launch the Map app with parameters.

The new XAML will look like this:

<TextBlock Grid.Column="0" Grid.Row="6" TextWrapping="Wrap" 
	 Style="{StaticResource BasicTextStyle}" 
	 HorizontalAlignment="Left" VerticalAlignment="Center">Latitude
</TextBlock> 
<TextBox Grid.Column="1" Grid.Row="6" Margin="0,0,10,10" x:Name="LatTextbox" 
         HorizontalAlignment="Left"  Text="" Width="300" /> 

<
TextBlock Grid.Column="0" Grid.Row="7" TextWrapping="Wrap" Style="{StaticResource BasicTextStyle}" HorizontalAlignment="Left" VerticalAlignment="Center">Longitude</TextBlock> <TextBox Grid.Column="1" Grid.Row="7" Margin="0,0,10,10" x:Name="LonTextbox" HorizontalAlignment="Left" Text="" Width="300" />
<Button x:Name="myPhotoLoc" Grid.Column="1" Grid.Row="8" 
        Click="myPhotoLoc_Click" Width="300" Margin="0,0,10,0" >Open Map</Button> 

Next we look at the code behind and in the function GetImagePropertiesForDisplay. We replace the code that splits the coordinates in degrees, minutes and seconds with the snippet below.

' Do a simple check if GPS data exists. 
   If  (m_imageProperties.Latitude.HasValue) AndAlso  (m_imageProperties.Longitude.HasValue) Then 
     LatText = Math.Round(m_imageProperties.Latitude.Value,  6).ToString
     LatTextbox.Text = LatText
  LonText = Math.Round(m_imageProperties.Longitude.Value,  6).ToString
  LonTextbox.Text = LonText
 End If 

We also introduce a new function to handle the click-event on our button and launch the Map app:

Private Async Sub myPhotoLoc_Click(sender As Object, e As RoutedEventArgs)
  ' Create the URI to launch  from a string. 
  Dim uri = New Uri("bingmaps:?collection=name.Photo%20Locations~point." + _
     LatText + "_" + LonText + "_My%20Photo&sty=a")
  ' Launch the URI. 
  Dim success As Boolean = Await Windows.System.Launcher.LaunchUriAsync(uri)
End Sub 

And that’s it. You’ll find the complete source code here.

Happy Mapping ☺

PhotoViewer