Click here to Skip to main content
15,892,809 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 
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 
Still looking for an answer of how you tell me how to detect when location is set in your code example and how to use it downstream in my code? This is my primary question.

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

PlotCurrentLocationOnMap(location) (how do I ensure this doesn't happen until location is set). I don't want to block the UI. Can I somehow put it in the lambda expression?

If I set the Movement Threshold to 100 when instantiating the watcher, will I be within 100m of my location or is that based on the GeoPositionAccuracy setting (default/high).

Regarding power, I currently am using such a watcher object to continually receive location information and process it when my app is running. The reason for my question was perhaps I can prolong battery life by simply asking for the current location periodically or only trigger when I sense movement over my current approach. In a practical sense, instead of dropping a piece of popcorn every 100m, I can ask every 30 seconds. Just wondering if one way better than the other.
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.