IT tutorials
 
Mobile
 

Windows Phone 8 : Building Location Aware Apps - A Walkthrough of the Location Viewer Sample (part 1) - GeoLocationViewModel Class

1/27/2015 3:09:37 AM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

GeoLocationViewModel Class

The GeoLocationViewModel.Start method instantiates either the GeolocatorProxy or a MockGeoLocator depending on the value of the viewmodel’s Boolean useMockLocator field (see Listing 1).


Tip

Rather than requiring the viewmodel to determine which IGeoLocator implementation it should use, Inversion of Control (IoC) could be employed to resolve the object.


The Start method subscribes to the PositionChanged and StatusChanged events of the IGeoLocator, and then calls the IGeoLocator object’s Start method to begin receiving location notifications.

When the PositionChanged event is raised, the viewmodel’s GeoCoordinate property is set to the current location.

LISTING 1. GeoLocationViewModel.Start Method (excerpt)


void Start()
{
    if (running)
    {
        return;
    }

    Running = true;
    CanStart = false;

    if (useMockLocator)
    {
        geoLocator = new MockGeoLocator();
    }
    else
    {
        geoLocator = new GeolocatorProxy
            {
                MovementThreshold = 20,
                DesiredAccuracy = PositionAccuracy.High,
                ReportInterval = 1000
            };
    }
...
    geoLocator.PositionChanged
        += (o, args) =>
            {
                GeoCoordinate coordinate = args.Position.GeoCoordinate;
                GeoCoordinate = coordinate;
                ResolveAddress(coordinate);
            };

    geoLocator.StatusChanged
        += (o, args) => PositionStatus = args.Status;


    geoLocator.Start();
}


The viewmodel contains two DelegateCommands that are used to start and stop position monitoring. The commands are initialized in the viewmodel constructor, as shown:

public GeoLocationViewModel()
{
    startCommand = new DelegateCommand(obj => Start(), arg => CanStart);
    stopCommand = new DelegateCommand(obj => Stop(), arg => Running);
    PropertyChanged += delegate { RefreshCommands(); };
}

Tapping the Stop button causes the viewmodel’s StopCommand to execute, which calls the Stop method of the geoLocator. This prevents the PositionChanged event from being raised. The Stop method is shown in the following excerpt:

void Stop()
{
    if (!running || geoPositionWatcher == null)
    {
        return;
    }

    if (sampler != null)
    {
        sampler.Dispose();
        sampler = null;
    }

    geoPositionWatcher.Stop();
    Running = false;
    CanStart = true;
}

 
Others
 
- Windows Phone 8 : Building Location Aware Apps - Getting Started with Location (part 3) - Code-Driven Location Simulation
- Windows Phone 8 : Building Location Aware Apps - Getting Started with Location (part 2) - Background Location Tracking
- Windows Phone 8 : Building Location Aware Apps - Getting Started with Location (part 1) - Geolocator Class
- Windows Phone 8 : Building Location Aware Apps - Location Sensing Technologies
- Windows Phone 8 : Range Controls (part 2) - Progress Indicator, Slider , ScrollBar
- Windows Phone 8 : Range Controls (part 1) - ProgressBar
- Windows Phone 8 : Items Controls - ListBox, ComboBox
- Windows Phone 8 : LinkedIn (part 3) - Posting a Message, The Feeds Filter
- Windows Phone 8 : LinkedIn (part 2) - Accessing LinkedIn Content, Responding to a LinkedIn Post
- Windows Phone 8 : LinkedIn (part 1) - Configuring Your LinkedIn Account
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us