×
Namespaces

Variants
Actions
(Difference between revisions)

How to use Cimbalino Windows Phone Toolkit Location

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Tidy code examples)
hamishwillee (Talk | contribs)
m (Hamishwillee - fix typo)
Line 1: Line 1:
 
[[Category:Location on Windows Phone]][[Category:Cimbalino Windows Phone Toolkit]][[Category:MVVM Light Toolkit]][[Category:How To]][[Category:XAML]][[Category:Windows Phone 7.5]][[Category:Windows Phone 8]]
 
[[Category:Location on Windows Phone]][[Category:Cimbalino Windows Phone Toolkit]][[Category:MVVM Light Toolkit]][[Category:How To]][[Category:XAML]][[Category:Windows Phone 7.5]][[Category:Windows Phone 8]]
{{Abstract|This article shows how to access locations information using {{Icode|LocationService}} from the [http://cimbalino.org/ Cimbalino Windows Phone Toolkit].}}
+
{{Abstract|This article shows how to access location information using {{Icode|LocationService}} from the [http://cimbalino.org/ Cimbalino Windows Phone Toolkit].}}
  
 
{{ArticleMetaData <!-- v1.3 -->
 
{{ArticleMetaData <!-- v1.3 -->
Line 25: Line 25:
 
The ''Cimbalino Windows Phone Toolkit'' delivers a set of useful and powerful [http://en.wikipedia.org/wiki/Model_View_ViewModel MVVM]-compatible tools and services to help developers build Silverlight applications for Windows Phone. The Toolkit is divided in projects which deliver different features, ranging from base MVVM services and helpers, through to code for background agents, and for accessing media library, location services etc. Cimbalino.Phone.Toolkit.Location is a MVVM compatible services for location access.
 
The ''Cimbalino Windows Phone Toolkit'' delivers a set of useful and powerful [http://en.wikipedia.org/wiki/Model_View_ViewModel MVVM]-compatible tools and services to help developers build Silverlight applications for Windows Phone. The Toolkit is divided in projects which deliver different features, ranging from base MVVM services and helpers, through to code for background agents, and for accessing media library, location services etc. Cimbalino.Phone.Toolkit.Location is a MVVM compatible services for location access.
  
[https://github.com/Cimbalino/Cimbalino-Phone-Toolkit/blob/master/src/Cimbalino.Phone.Toolkit.Location%20(WP71)/Services/ILocationService.cs ILocationService] - Represents an interface for a service capable of handling the device location capabilities. The implementation is [https://github.com/Cimbalino/Cimbalino-Phone-Toolkit/blob/master/src/Cimbalino.Phone.Toolkit.Location%20(WP71)/Services/LocationService.cs LocationService].
+
[http://github.com/Cimbalino/Cimbalino-Phone-Toolkit/blob/master/src/Cimbalino.Phone.Toolkit.Location%20(WP71)/Services/ILocationService.cs ILocationService] - Represents an interface for a service capable of handling the device location capabilities. The implementation is [http://github.com/Cimbalino/Cimbalino-Phone-Toolkit/blob/master/src/Cimbalino.Phone.Toolkit.Location%20(WP71)/Services/LocationService.cs LocationService].
  
 
This article explains how to use the service in an [http://mvvmlight.codeplex.com/ MVVM Light] app. Developers should first read [[How to use MVVM Light Toolkit for Windows Phone#How to use services|How to use MVVM Light Toolkit for Windows Phone]] for a general overview of MVVM Light and how to use services.
 
This article explains how to use the service in an [http://mvvmlight.codeplex.com/ MVVM Light] app. Developers should first read [[How to use MVVM Light Toolkit for Windows Phone#How to use services|How to use MVVM Light Toolkit for Windows Phone]] for a general overview of MVVM Light and how to use services.

Revision as of 07:40, 27 November 2013

This article shows how to access location information using LocationService from the Cimbalino Windows Phone Toolkit.

SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
Tested with
SDK: Windows Phone 8.0 SDK, Windows Phone 7.1.1. SDK
Devices(s): Lumia 800, Lumia 920
CompatibilityPlatform Security
Capabilities: ID_CAP_LOCATION
Article
Created: saramgsilva (18 Nov 2013)
Last edited: hamishwillee (27 Nov 2013)

Contents

Introduction

The Cimbalino Windows Phone Toolkit delivers a set of useful and powerful MVVM-compatible tools and services to help developers build Silverlight applications for Windows Phone. The Toolkit is divided in projects which deliver different features, ranging from base MVVM services and helpers, through to code for background agents, and for accessing media library, location services etc. Cimbalino.Phone.Toolkit.Location is a MVVM compatible services for location access.

ILocationService - Represents an interface for a service capable of handling the device location capabilities. The implementation is LocationService.

This article explains how to use the service in an MVVM Light app. Developers should first read How to use MVVM Light Toolkit for Windows Phone for a general overview of MVVM Light and how to use services.


Building the example code

The source code for the code example is available here: (github).

To build the source code you will also need the MVVM Light Toolkit and the Cimbalino Windows Phone Toolkit. Their packages are available in the Nuget Package Manager. For more details see:


The sample is built using MVVM Light and the article does not go into great detail about the plumbing/infrastructure provided by the toolkit. For more information see:


Note.pngNote: you must specify the following capabilities in the app manifest: ID_CAP_LOCATION


Registering the service

Register the service in the ViewModelLocator constructor as shown below

  1.     /// This class contains static references to all the view models in the
  2.     /// application and provides an entry point for the bindings.
  3.     public class ViewModelLocator
  4.     {
  5.         /// Initializes a new instance of the ViewModelLocator class.
  6.         public ViewModelLocator()
  7.         {
  8.             ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
  9.             if (!SimpleIoc.Default.IsRegistered<ILocationService>())
  10.             {
  11.                 SimpleIoc.Default.Register<ILocationService, LocationService>();
  12.             }
  13.             SimpleIoc.Default.Register<MainViewModel>();
  14.         }
  15.  
  16.  
  17.         /// Gets the main view model.
  18.         public MainViewModel MainViewModel
  19.         {
  20.             get
  21.             {
  22.                 return ServiceLocator.Current.GetInstance<MainViewModel>();
  23.             }
  24.         }
  25.  
  26.         public static void Cleanup()
  27.         {
  28.             // TODO Clear the ViewModels
  29.             var viewModelLocator = (ViewModelLocator)App.Current.Resources["Locator"];
  30.             viewModelLocator.MainViewModel.Cleanup();
  31.         }
  32.     }


Implementing the ViewModel

Then we should implement the MainViewModel.cs class as following:

  1.    using System.Windows;
  2.     using System.Windows.Input;
  3.     using System.Windows.Threading;
  4.  
  5.     using Cimbalino.Phone.Toolkit.Services;
  6.  
  7.     using GalaSoft.MvvmLight.Command;
  8.     using GalaSoft.MvvmLight;
  9.  
  10.     /// This class contains properties that the main View can data bind to.
  11.     public class MainViewModel : ViewModelBase
  12.     {
  13.         /// The location service
  14.         private readonly ILocationService _locationService;
  15.  
  16.         private bool _isLocationEnable;
  17.  
  18.         /// Define if is start enable
  19.         private bool _isStartEnable;
  20.  
  21.         /// Define if is stop enable
  22.         private bool _isStopEnable;
  23.  
  24.         /// The latitude
  25.         private double _latitude;
  26.  
  27.         /// The logitude
  28.         private double _longitude;
  29.  
  30.         /// The status
  31.         private LocationServiceStatus _status;
  32.  
  33.         /// Initializes a new instance of the MainViewModel class.
  34.         public MainViewModel(ILocationService locationService)
  35.         {
  36.             IsStartEnable = true;
  37.             IsLocationEnable = true;
  38.             IsStopEnable = false;
  39.             _locationService = locationService;
  40.             _locationService.ReportInterval = 5;
  41.             _locationService.PositionChanged += LocationService_PositionChanged;
  42.             _locationService.StatusChanged += LocationService_StatusChanged;
  43.             StartCommand =new RelayCommand(Start);
  44.             StopCommand =new RelayCommand(Stop);
  45.             LocationCommand = new RelayCommand(GetLocation);
  46.         }
  47.  
  48.         /// Gets or sets a value indicating whether [is location enable].
  49.         public bool IsLocationEnable
  50.         {
  51.             get
  52.             {
  53.                 return this._isLocationEnable;
  54.             }
  55.             set
  56.             {
  57.                 Set("IsLocationEnable", ref _isLocationEnable, value);
  58.             }
  59.         }
  60.  
  61.  
  62.         /// Gets or sets a value indicating whether [is start enable].
  63.         public bool IsStartEnable
  64.         {
  65.             get
  66.             {
  67.                 return this._isStartEnable;
  68.             }
  69.             set
  70.             {
  71.                 Set("IsStartEnable", ref _isStartEnable, value);
  72.             }
  73.         }
  74.  
  75.         /// Gets or sets a value indicating whether [is stop enable].
  76.         public bool IsStopEnable
  77.         {
  78.             get
  79.             {
  80.                 return this._isStopEnable;
  81.             }
  82.             set
  83.             {
  84.                 Set("IsStopEnable", ref _isStopEnable, value);
  85.             }
  86.         }
  87.  
  88.         /// Gets or sets the latitude.
  89.         public double Latitude
  90.         {
  91.             get
  92.             {
  93.                 return _latitude;
  94.             }
  95.             set
  96.             {
  97.                 Set("Latitude", ref _latitude, value);
  98.             }
  99.         }
  100.  
  101.         /// Gets or sets the location command.
  102.         public ICommand LocationCommand { get; private set; }
  103.  
  104.         /// Gets or sets the longitude.
  105.         public double Longitude
  106.         {
  107.             get
  108.             {
  109.                 return this._longitude;
  110.             }
  111.             set
  112.             {
  113.                 Set("Longitude", ref _longitude, value);
  114.             }
  115.         }
  116.  
  117.         /// Gets or sets the start command.
  118.         public ICommand StartCommand { get; private set; }
  119.  
  120.         /// Gets or sets the status.
  121.         public LocationServiceStatus Status
  122.         {
  123.             get
  124.             {
  125.                 return _status;
  126.             }
  127.             set
  128.             {
  129.                 Set("Status", ref _status, value);
  130.             }
  131.         }
  132.  
  133.         /// Gets or sets the stop command.
  134.         public ICommand StopCommand { get; private set; }
  135.  
  136.         /// Unregisters this instance from the Messenger class.
  137.         public override void Cleanup()
  138.         {
  139.             base.Cleanup();
  140.             _locationService.PositionChanged -= LocationService_PositionChanged;
  141.             _locationService.StatusChanged -= LocationService_StatusChanged;
  142.         }
  143.  
  144.         /// Gets the location.
  145.         private async void GetLocation()
  146.         {
  147.             var result = await _locationService.GetPositionAsync();
  148.             Longitude = result.Longitude;
  149.             Latitude = result.Latitude;
  150.         }
  151.  
  152.         /// Handles the PositionChanged event of the LocationService control.
  153.         private void LocationService_PositionChanged(object sender, LocationServicePositionChangedEventArgs e)
  154.         {
  155.             Deployment.Current.Dispatcher.BeginInvoke(delegate
  156.                 {
  157.                     Latitude = e.Position.Latitude;
  158.                     Longitude = e.Position.Longitude;
  159.                 });
  160.         }
  161.  
  162.         /// Handles the StatusChanged event of the _locationService control.
  163.         private void LocationService_StatusChanged(object sender, LocationServiceStatusChangedEventArgs e)
  164.         {
  165.             Deployment.Current.Dispatcher.BeginInvoke(delegate { Status = e.Status; });
  166.         }
  167.  
  168.         /// Starts the location service.
  169.         private void Start()
  170.         {
  171.             IsStartEnable = false;
  172.             IsStopEnable = true;
  173.             IsLocationEnable = false;
  174.             _locationService.Start();
  175.         }
  176.  
  177.         /// Stops the location service.
  178.         private void Stop()
  179.         {
  180.             IsLocationEnable = true;
  181.             IsStartEnable = true;
  182.             IsStopEnable = false;
  183.             _locationService.Stop();
  184.         }
  185.     }

Implementing the View

Add the binding in main page like:

DataContext="{Binding MainViewModel, Source={StaticResource Locator}}"

The MainPage.xaml can be the following:

  1. <phone:PhoneApplicationPage x:Class="CimbalinoSample.MainPage"
  2.                             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.                             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.                             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5.                             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6.                             xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
  7.                             xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
  8.                             DataContext="{Binding MainViewModel,
  9.                                                   Source={StaticResource Locator}}"
  10.                             FontFamily="{StaticResource PhoneFontFamilyNormal}"
  11.                             FontSize="{StaticResource PhoneFontSizeNormal}"
  12.                             Foreground="{StaticResource PhoneForegroundBrush}"
  13.                             Orientation="Portrait"
  14.                             SupportedOrientations="Portrait"
  15.                             shell:SystemTray.IsVisible="True"
  16.                             mc:Ignorable="d">
  17.  
  18.     <!--  LayoutRoot is the root grid where all page content is placed  -->
  19.     <Grid x:Name="LayoutRoot" Background="Transparent">
  20.         <Grid.RowDefinitions>
  21.             <RowDefinition Height="Auto" />
  22.             <RowDefinition Height="*" />
  23.         </Grid.RowDefinitions>
  24.  
  25.         <!--  TitlePanel contains the name of the application and page title  -->
  26.         <StackPanel x:Name="TitlePanel"
  27.                     Grid.Row="0"
  28.                     Margin="12,17,0,28">
  29.             <TextBlock Margin="12,0"
  30.                        Style="{StaticResource PhoneTextTitle2Style}"
  31.                        Text="Cimbalino Sample" />
  32.             <TextBlock Margin="9,-7,0,0"
  33.                        Style="{StaticResource PhoneTextTitle1Style}"
  34.                        Text="Location" />
  35.         </StackPanel>
  36.  
  37.         <!--  ContentPanel - place additional content here  -->
  38.         <Grid x:Name="ContentPanel"
  39.               Grid.Row="1"
  40.               Margin="12,0,12,0">
  41.             <TextBlock TextWrapping="Wrap">
  42.                 Latitude:<Run Text="{Binding Latitude}" />
  43.             </TextBlock>
  44.             <TextBlock Margin="0,51,0,-51" TextWrapping="Wrap">
  45.                 Logitude:<Run Text="{Binding Longitude}" />
  46.             </TextBlock>
  47.             <TextBlock Margin="0,102,0,-102" TextWrapping="Wrap">
  48.                 Status:<Run Text="{Binding Status}" />
  49.             </TextBlock>
  50.             <Button Margin="0,298,0,214"
  51.                     IsEnabled="{Binding IsStartEnable}"
  52.                     Command="{Binding StartCommand}"
  53.                     Content="Start" />
  54.             <Button Height="76"
  55.                     IsEnabled="{Binding IsStopEnable}"
  56.                     Margin="0,0,0,138"
  57.                     VerticalAlignment="Bottom"
  58.                     Command="{Binding StopCommand}"
  59.                     Content="Stop" />
  60.             <Button Margin="0,219,0,293"
  61.                     IsEnabled="{Binding IsLocationEnable}"
  62.                     Command="{Binding LocationCommand}"
  63.                     Content="Get location" />
  64.         </Grid>
  65.     </Grid>
  66. </phone:PhoneApplicationPage>
94 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×