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:
- Part 1 – Wireframe
- Part 2 – Composites
- Part 3 – Icon Sizes
- 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?
#define 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();
#else
_watcher = new System.Device.Location.GeoCoordinateWatcher();
#endif
_watcher.PositionChanged += _watcher_PositionChanged;
}
bool started = _watcher.TryStart(true, TimeSpan.FromSeconds(LocationServices.GPS_TIMEOUT));
if (!started) return null;
// Wait until GeoCoordinate Data is set
_syncLocation.WaitOne();
_watcher.Stop();
_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
_syncLocation.Set();
}
}
}
And this is how you will use the above code:
// 1. Test Retriever
var retriever = new GeoCoordRetriever();
var coord = retriever.ForCurrentLocation();
***IMPORTANT NOTE:
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 :)