Click here to Skip to main content
15,893,622 members
Articles / Operating Systems / Windows

A Helper Class to Get the Current Location on a Windows Phone Just Once

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
15 Jan 2011CPOL 27.2K   6   14
Here is a helper class to get the current location on a Windows Phone just once

Getting the location on Windows Phone is done via the GeoCoordinateWatcher. The coordinate watcher has some best practices associated with it one of which is to minimize power consumption. Once you turn this thing on, it's going to be using the GPS to stream location event back to your app which will hit the battery.

There are some instances where you may note a running notification of the current portion but just want the current position (for instance, you may want to initialize the position on a map but not track changes to position).

So this is a small helper class that starts up the coordinate watcher asynchronously, returns the first coordinate found and then shuts it down. One could use TryStart like so:

C#
using (var watcher =  new 
                GeoCoordinateWatcher(GeoPositionAccuracy.Default))
{
    var location = watcher.TryStart
                (false, TimeSpan.FromMilliseconds(1000));
}

but since that will block your UI thread (assuming you call it from the UI) you'd need to wrap it in some asynchronous stuff anyways.
So I use this class:

C#
public class ImmediateLocation : IDisposable
{
    private GeoCoordinateWatcher _watcher;
    private Action<GeoCoordinate> _action;

    public ImmediateLocation(Action<GeoCoordinate> a)
    {
        Debug.Assert(a != null);

        _action = a;
    }

    public void GetLocation()
    {
        if (_watcher == null)
        {
            _watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
            _watcher.MovementThreshold = 1000;

            _watcher.PositionChanged += new 
                EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>
                (_watcher_PositionChanged);
            _watcher.StatusChanged += new 
                EventHandler<GeoPositionStatusChangedEventArgs>
                (_watcher_StatusChanged);

            _watcher.Start(false);

            if (_watcher.Status == GeoPositionStatus.Disabled
                || _watcher.Permission == GeoPositionPermission.Denied)
                Dispose();
        }
    }

    void _watcher_StatusChanged(object sender, 
        GeoPositionStatusChangedEventArgs e)
    {
        if (e.Status == GeoPositionStatus.Disabled 
            || _watcher.Permission == GeoPositionPermission.Denied)
            Dispose();
    }

    void _watcher_PositionChanged(object sender, 
        GeoPositionChangedEventArgs<GeoCoordinate> e)
    {
        _action(e.Position.Location);
        Dispose();
    }

    public void Dispose()
    {
        if (_watcher != null)
        {
            _watcher.Stop();
            _watcher.PositionChanged -= new 
                EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>
                (_watcher_PositionChanged);
            _watcher.StatusChanged -= new 
                EventHandler<GeoPositionStatusChangedEventArgs>
                (_watcher_StatusChanged);
            _watcher.Dispose();
        }
        _watcher = null;
        _action = null;
    }
} 

Then you can just do this:

C#
var immediate = new ImmediateLocation(x => location = x);
immediate.GetLocation();

GeoCoordinate location;

It acts like a one time use, fire and forget location setter that cleans itself up when done. It does have Dispose if you want to manage the lifetime more closely.

This article was originally posted at http://spookycoding.blogspot.com/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Starkey Laboratories
United States United States
The first computer program I ever wrote was in BASIC on a TRS-80 Model I and it looked something like:
10 PRINT "Don is cool"
20 GOTO 10

It only went downhill from there.

Hey look, I've got a blog

Comments and Discussions

 
QuestionHow to get the location? Pin
Saggaf Arsyad16-Dec-12 21:06
Saggaf Arsyad16-Dec-12 21:06 
GeneralMy vote of 5 Pin
Colin Eberhardt24-Nov-12 8:21
Colin Eberhardt24-Nov-12 8:21 
QuestionHow to consume result / battery tradeoff of start/dispose vs leave it running / accuracy control Pin
Member 459906229-Aug-12 0:47
Member 459906229-Aug-12 0:47 
Great article. Got it working but confused how to integrate into my code.

Suppose I want to use the result of the immediate.GetLocation() by using result downstream in my code. Can you describe further how to consume results? I am just getting started in lambda expression so this may be a simple/stupid question.

GeoCoordinate location ; <== I moved this here to delcare prior to usage
var immediate = new ImmediateLocation(x => location = x);
immediate.GetLocation() ;

// Where do I put this following line of code to execute after location is set.

PlotMyLocationOnMap(location) ;

// In current spot always gets executed with a null location.
// Should I fire off an event when location is determined in ImmediateLocation class?

=================

I am also intererested in hearing thoughts on the battery consumption / performance of leaving it running for say 10-30 minutes vs firing off GetLocation say every 15 seconds to track your movement. I beleive each time it starts it has to "hone" in on your location? Is there a "startup cost" / "delay" or loss of accuracy each time?

=================

Does the MovementThreshold setting control the accuracy at startup? In article says 1000. If I change to 50, will that return a location within 50 meters?
Fast Eddie

AnswerRe: How to consume result / battery tradeoff of start/dispose vs leave it running / accuracy control Pin
Don Kackman29-Aug-12 9:52
Don Kackman29-Aug-12 9:52 
QuestionRe: How to consume result / battery tradeoff of start/dispose vs leave it running / accuracy control Pin
Member 459906229-Aug-12 10:46
Member 459906229-Aug-12 10:46 
GeneralThe code always returns a null on a real WP7 Pin
Orrin4-Mar-11 19:21
Orrin4-Mar-11 19:21 
GeneralAlways returns a null Pin
Orrin15-Jan-11 9:28
Orrin15-Jan-11 9:28 
GeneralAlways returns a null Pin
Orrin15-Jan-11 8:49
Orrin15-Jan-11 8:49 
GeneralRe: Always returns a null Pin
Don Kackman15-Jan-11 9:02
Don Kackman15-Jan-11 9:02 
GeneralRe: Always returns a null Pin
Don Kackman15-Jan-11 9:04
Don Kackman15-Jan-11 9:04 
GeneralRe: Always returns a null Pin
Orrin15-Jan-11 10:48
Orrin15-Jan-11 10:48 
GeneralRe: Always returns a null Pin
Don Kackman15-Jan-11 12:32
Don Kackman15-Jan-11 12:32 
GeneralRe: Always returns a null Pin
Orrin15-Jan-11 12:53
Orrin15-Jan-11 12:53 
GeneralMy vote of 5 Pin
maq_rohit9-Dec-10 21:59
professionalmaq_rohit9-Dec-10 21:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.