Over a million developers have joined DZone.

Windows Phone 8 and Continuous Location Tracking

DZone's Guide to

Windows Phone 8 and Continuous Location Tracking

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

While digging through the Windows Phone 8 SDK docs, I noticed that the Location services seem to have changed to include an option for tracking the handset's location on a continuous basis. It is marked in the docs as new to WP8 so I thought I would share a sample.

Source is available here.

    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock Text="{Binding Path=LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock Text="{Binding Path=LocalizedResources.PageTitle, Source={StaticResource LocalizedStrings}}" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
                <Button x:Name="TrackLocationButton" Click="TrackLocation_Click" Content="track location"/>
                <TextBlock x:Name="LatitudeTextBlock"/>
                <TextBlock x:Name="LongitudeTextBlock"/>
                <TextBlock x:Name="StatusTextBlock"/>

The following code is pulled almost directly from the SDK documentation. I tested it in the emulator and found it works well.

using System.Windows;
using Microsoft.Phone.Controls;
using Windows.Devices.Geolocation;
namespace ContinuousLocation
    public partial class MainPage : PhoneApplicationPage
        Geolocator geolocator = null;
        bool tracking = false;
        // Constructor
        public MainPage()
        private void TrackLocation_Click(object sender, RoutedEventArgs e)
            if (!tracking)
                geolocator = new Geolocator();
                geolocator.DesiredAccuracy = PositionAccuracy.High;
                geolocator.MovementThreshold = 100; // The units are meters.
                geolocator.StatusChanged += geolocator_StatusChanged;
                geolocator.PositionChanged += geolocator_PositionChanged;
                tracking = true;
                TrackLocationButton.Content = "stop tracking";
                geolocator.PositionChanged -= geolocator_PositionChanged;
                geolocator.StatusChanged -= geolocator_StatusChanged;
                geolocator = null;
                tracking = false;
                TrackLocationButton.Content = "track location";
                StatusTextBlock.Text = "stopped";
        void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
            Dispatcher.BeginInvoke(() =>
                LatitudeTextBlock.Text = args.Position.Coordinate.Latitude.ToString(  "0.00000000");
                LongitudeTextBlock.Text = args.Position.Coordinate.Longitude.ToString("0.00000000");
        void geolocator_StatusChanged(Geolocator sender, StatusChangedEventArgs args)
            string status = "";
            switch (args.Status)
                case PositionStatus.Disabled:
                    // the application does not have the right capability or the location master switch is off
                    status = "location is disabled in phone settings";
                case PositionStatus.Initializing:
                    // the geolocator started the tracking operation
                    status = "initializing";
                case PositionStatus.NoData:
                    // the location service was not able to acquire the location
                    status = "no data";
                case PositionStatus.Ready:
                    // the location service is generating geopositions as specified by the tracking parameters
                    status = "ready";
                case PositionStatus.NotAvailable:
                    status = "not available";
                    // not used in WindowsPhone, Windows desktop uses this value to signal that there is no hardware capable to acquire location information
                case PositionStatus.NotInitialized:
                    // the initial state of the geolocator, once the tracking operation is stopped by the user the geolocator moves back to this state
            Dispatcher.BeginInvoke(() =>
                StatusTextBlock.Text = status;

 Now the final step is to enable the location capability in the WMAppManifest.xml file:


And now when you run it in the emulator, you click the button and the coordinates are displayed. When you open the Location tab of the tools and click on a map location, the app displays the new location in a moment.

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.


Published at DZone with permission of Adam Benoit, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}