Displaying Location Using the GeoLocationView Page
The view has two buttons, which are bound to the viewmodel’s StartCommand
and StopCommand
(see Listing 1).
The GeoLocationView
contains several TextBlock
elements that are bound to viewmodel properties.
A Slider
is bound to the viewmodel’s SampleIntervalMs
property, allowing the user to control the sample frequency.
A Windows Phone Toolkit ToggleSwitch
is bound to the viewmodel’s UseMockLocator
property and changes the IGeoLocator
implementation that is used during tracking.
LISTING 1. GeoLocationView
Page (excerpt showing main content panel)
<StackPanel Grid.Row="1" Margin="21,30,21,0">
<StackPanel Grid.Row="1" Margin="21,0,21,0">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60" />
<RowDefinition Height="60" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Text="latitude"
Style="{StaticResource LatLongTitleStyle}"
VerticalAlignment="Center" />
<TextBlock Text="{Binding GeoCoordinate.Latitude, StringFormat=\{0:F4\}}"
Style="{StaticResource LatLongStyle}"
Grid.Row="1"/>
<TextBlock Text="longitude"
Style="{StaticResource LatLongTitleStyle}"
Grid.Column="1" VerticalAlignment="Center" />
<TextBlock Text="{Binding GeoCoordinate.Longitude, StringFormat=\{0:F4\}}"
Style="{StaticResource LatLongStyle}"
Grid.Row="1" Grid.Column="1" />
</Grid>
<TextBlock Text="{Binding PositionStatus}"
Style="{StaticResource PhoneTextNormalStyle}"
Foreground="{StaticResource PhoneAccentBrush}"
Height="50" Margin="0" />
<TextBlock Text="sample interval in milliseconds (0 for realtime)"
Style="{StaticResource LabelTextStyle}"
FontSize="{StaticResource PhoneFontSizeSmall}"/>
<Slider Minimum="0"
Maximum="10000"
Value="{Binding SampleIntervalMs, Mode=TwoWay}"
IsEnabled="{Binding CanStart}"
LargeChange="1000" SmallChange="1000"/>
<TextBlock Text="{Binding SampleIntervalMs}" Width="100" Height="50"
HorizontalAlignment="Left"
Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock Text="{Binding Message}" Height="50"
Style="{StaticResource PhoneTextNormalStyle}"/>
<toolkit:ToggleSwitch Header="mock locator"
IsChecked="{Binding UseMockLocator, Mode=TwoWay}"
IsEnabled="{Binding CanStart}"/>
<StackPanel Orientation="Horizontal">
<Button Content="Start"
Command="{Binding StartCommand}"
Margin="0,20,0,0" Width="150" HorizontalAlignment="Left"/>
<Button Content="Stop"
Command="{Binding StopCommand}"
Margin="0,20,0,0" Width="150" HorizontalAlignment="Left"/>
</StackPanel>
</StackPanel>
Tapping the Start button causes the current location to be displayed whenever the IGeoLocator
object’s PositionChanged
event is raised (see Figure 1).
FIGURE 1 GeoLocationView
page.
The OS displays an indicator in the
system tray when location tracking is engaged. In the sample this
occurs when the mock locator is turned off.
Civic Address Resolution
CivicAddressResolver
is supposed to be able to resolve a street address from a geographic
coordinate. It has not, however, been fully implemented yet and is
unable to resolve an address.
Note
In the 8.0 release of the Windows Phone OS, the CivicAddressResolver
class has not been fully implemented.
The GeoLocationViewModel
class demonstrates how to use the CivicAddressResolver
, but the CivicAddressResolver.ResolveAddressCompleted
handler always receives an unknown address value.