WP7: Dissecting we.Muslim app | Part 5 – Get Current Latitude Longitude

In this part we will use Location Services to get current coordinates. I’ve created a class that you can reuse and it works with the GPS Emulator too.

Previous parts in this series:

  1. Part 1 – Wireframe
  2. Part 2 – Composites
  3. Part 3 – Icon Sizes
  4. Part 4 – Proper Splash Screen


Set up GPS Emulator

First click here to download GPS Emulator for Windows Phone 7.

Next right-click your project and Add Reference to the GpsEmulatorClient.DLL file found under \GpsEmulator\GpsEmulator_AppHubReady\GpsEmulator\GpsEmulatorClient\Bin\Debug folder from the zip.

Before we run our Windows Phone project, you need to run the GPS Emulator First. It is under \GpsEmulator\GpsEmulator_AppHubReady\GpsEmulator\GpsEmulator\bin\Debug folder from the zip. Make sure you right-click and Run as Administrator.

Let’s change the default Redmond Location, enter this coordinate:

  • Latitude: 24.47
  • Longitude: 54.37

Then click Update Point


What you need to monitor is the status at the bottom-right corder of the GPS Emulator:


Make sure before you spend hours debugging that your Windows Phone project is actually connected to the GPS Emulator. How do we do that? Let’s move on to the next section…


GPS Emulator & Device Location Class Wrapper

Following the Single-Responsibility Principle (SRP), I’ve created a class that handles getting the current coordinates from either GPS Emulator or Physical Device Location Services. Due to the nature of TryStart() that blocks the current thread, make sure you run this inside a BackgroundWorker component.

// Are we using GPS Emulator?
using System;
using System.Device.Location;
using System.Threading;
namespace we.Muslim.Utils
  public class GeoCoordRetriever
    private IGeoPositionWatcher<GeoCoordinate> _watcher;
    private readonly ManualResetEvent _syncLocation = new ManualResetEvent(false);
    private GeoCoordinate _coordinate;
    public GeoCoordinate ForCurrentLocation()
      if (_watcher == null)
      #if GPS_EMULATOR
          _watcher = new GpsEmulatorClient.GeoCoordinateWatcher();
          _watcher = new System.Device.Location.GeoCoordinateWatcher();
          _watcher.PositionChanged += _watcher_PositionChanged;
      bool started = _watcher.TryStart(true, TimeSpan.FromSeconds(LocationServices.GPS_TIMEOUT));
      if (!started) return null;
      // Wait until GeoCoordinate Data is set
      _watcher = null;
      return _coordinate;
    private void _watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
      // just need initial location
      _watcher.PositionChanged -= _watcher_PositionChanged;
      _coordinate = e.Position.Location;
      // Tell the other thread
      // that we're done setting GeoCoordinate


And this is how you will use the above code:

  // 1. Test Retriever 
  var retriever = new GeoCoordRetriever();
  var coord = retriever.ForCurrentLocation();



Make sure you comment out the first line above //#define GPS_EMULATOR when you deploy to real device, otherwise your phone will just hang :)


Now when you press F5 to debug on WP Emulator, you should see our Windows Phone is connected to the GPS Emulator:




Wait, how did I get that “Al Dhafrah, United Arab Emirates” text? Ah, that is the topic for next post tomorrow, cos I’m going to sleep now :)

Share this post: | | | |
Published Saturday, February 19, 2011 8:42 PM by zeddy
Filed under:


# Twitter Trackbacks for WP7: Dissecting we.Muslim app | Part 5 ??? Get Current Latitude Longitude - Z &amp; his Startup [netindonesia.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 WP7: Dissecting we.Muslim app | Part 5 ??? Get Current Latitude Longitude - Z &amp; his Startup         [netindonesia.net]        on Topsy.com

# re: WP7: Dissecting we.Muslim app | Part 5 – Get Current Latitude Longitude

Sunday, February 20, 2011 9:48 AM by zeddy

Whoops made a typo there...class renamed to "GeoCoordRetriever".

Geocode means to convert physical address to Latitude & Longitude value btw.

# WP7: Dissecting we.Muslim app | Part 6 – Get Area Name from Latitude Longitude

Monday, February 21, 2011 3:30 AM by Z & his Startup

Given a latitude longitude coordinate, let’s now resolve this to a meaningful Area, Country string. Previous

Powered by Community Server (Commercial Edition), by Telligent Systems