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
- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire

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
programming4us programming4us
 
Popular tags
 
Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS