×

First time here?

You are looking at the most recent posts. You may also want to check out older archives. Please leave a comment, ask a question and consider subscribing to the latest posts via RSS or email. Thank you for visiting!

If you’ve been excited about Windows Phone 7 development and the platform being Silverlight for application development, you probably rushed and downloaded all the tools (which are free by the way).  You may have even got the samples from the SDK and noticed the Location services example…but wondered why it doesn’t work.

If you are just getting started, I created some quickstart videos to help you through some of the basics.  You can view them here.

In case you haven’t figured it out: Location services (aka, GPS) is not emulated in the developer tools CTP. 

As you might expect, this makes it difficult to play around with location-based applications.

The API in Windows Phone 7 revolves around the GeoCoordinateWatcher class.  This class is what you would initialize to start listening for events:

   1: GeoCoordinateWatcher watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Low);
   2: watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
   3: watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);

As you can see, this watcher class looks for Status and Position changes.  The status is about the device peripheral itself (initializing, reading, etc.).  Position is more likely what you are interested in and would give you the details of where the device is reading the current location (longitude and latitude).  In the emulator right now the status will always return Disabled.

It’s relatively simple to simulate this, and here’s a really simple mock class for doing so.  Now, note this is not a complete emulation of the Location services APIs for Windows Phone 7 SDK.  This mock is to simply simulate a coordinate location and position changing.  The GeoLocationMock class implements the IGeoPositionWatcher<GeoCordinate> interface for mocking the location service.  There is a Start, Stop, PositionChanged and StatusChanged methods and events (TryStart is implemented, but simply calls Start).  To implement the mock in your application you would instantiate watcher (using above sample) as IGeoPositionWatcher<GeoCoordinate> instead of the GeoCoordinateWatcher specifically.  Here is a sample, and then an explanation:

   1: public partial class MainPage : PhoneApplicationPage
   2: {
   3:     IGeoPositionWatcher<GeoCoordinate> watcher;
   4: }
   5:  
   6: private void StartLocationService(GeoPositionAccuracy accuracy)
   7: {
   8:     if (watcher == null)
   9:     {
  10:         GeoCoordinateEventMock[] events = new GeoCoordinateEventMock[] {
  11:             new  GeoCoordinateEventMock { Latitude=34.4, Longitude=11.2, Time=new TimeSpan(0,0,5) },
  12:             new  GeoCoordinateEventMock { Latitude=31.4, Longitude=21.2, Time=new TimeSpan(0,0,1) },
  13:             new  GeoCoordinateEventMock { Latitude=34.3, Longitude=28.2, Time=new TimeSpan(0,0,2) },
  14:             new  GeoCoordinateEventMock { Latitude=32.4, Longitude=34.2, Time=new TimeSpan(0,0,3) },
  15:             new  GeoCoordinateEventMock { Latitude=31.2, Longitude=37.2, Time=new TimeSpan(0,0,4) },
  16:             new  GeoCoordinateEventMock { Latitude=33.73, Longitude=39.2, Time=new TimeSpan(0,0,5) },
  17:             new  GeoCoordinateEventMock { Latitude=31.87, Longitude=41.2, Time=new TimeSpan(0,0,6) },
  18:             new  GeoCoordinateEventMock { Latitude=11.81, Longitude=42.2, Time=new TimeSpan(0,0,7) }
  19:         };
  20:     
  21:         watcher = new EventListGeoLocationMock(events);
  22:         watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
  23:         watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
  24:     }
  25:  
  26:     watcher.Start();
  27: }

The ‘watcher’ is created using a list of geo location points in this sample above.  Now this could be some web service that does IP address reverse lookup or use hard-coded examples as well like I’ve done above.  Using this mock above and replacing it in the LocationServiceSample in the SDK, here’s what my screenshot shows:

Geo location services mock for Windows Phone 7

So you can see I can start the GPS emulation and simulate subtle position changes (or drastic ones if I wanted, aka maybe a social network map application of sorts).

Hopefully this little snippet will be valuable to play around with or expand upon for your needs.  If anything, you can create some emulation of the behavior temporarily.  The mock object used in a modified LocationServiceSample project can be downloaded here: LocationServiceSampleWithMock.zip.

Hope this helps!

UPDATE: Peter Torr actually had another geo location mock in his MIX10 talk which his code is now available for download.  It is much more comprehensive emulating accuracy, etc.  The above is a simpler approach, but both will get the job done depending on what you really need/want to emulate.


This work is licensed under a Creative Commons Attribution By license.


3/22/2010 10:52 AM | # re: Simulate Geo Location in Silverlight Windows Phone 7 emulator
Hi Ben -- 1) Camtasia; 2) there are many services out there that do this already, including Bing Maps API that will give you a centroid lat/long based on a zip code.
4/22/2010 4:51 PM | # re: Simulate Geo Location in Silverlight Windows Phone 7 emulator
Can Windows Phone 7 be used as a cyclocomputer? It's very similar to that of VeloComputer (velocomputer.com) for Android-based devices that support GPS.

I really want to forget about Android because with so many features of Windows Phone 7 that I really liked, it's going to make it hard for me to choose between Android and Windows Phone 7 when WP7 comes out.
7/31/2010 1:37 AM | # 
great info but I think there will always be a hurdle going against iphones and the like regardless of the new software and emulations.
8/8/2010 7:56 AM | # re: Simulate Geo Location in Silverlight Windows Phone 7 emulator
Great mock simulation. Thanks
9/18/2010 2:25 PM | # re: Simulate Geo Location in Silverlight Windows Phone 7 emulator
The emulator is Great including Bing Maps API then you can also see the centroid lat/long based on a zip code. I Love this emulator recommend this emulator
10/24/2010 4:03 AM | # re: Simulate Geo Location in Silverlight Windows Phone 7 emulator
Seems to not work with the latest SDK anymore. Get an {"Invalid cross-thread access."} exception in GeoLocationMock.cs OnTimerCallback
10/29/2010 10:10 PM | # re: Simulate Geo Location in Silverlight Windows Phone 7 emulator
Tim, great job. and this is the longest blog i have seen.
11/25/2010 10:33 PM | # re: Simulate Geo Location in Silverlight Windows Phone 7 emulator
I don't think that this type can challenge the same new product from another company. Farewell it's not really bad also.
12/17/2010 10:18 PM | # re: Simulate Geo Location in Silverlight Windows Phone 7 emulator
i am a new fresh person for silverlight.This is very good and fair.i wanna to download it
1/20/2011 8:24 AM | # re: Simulate Geo Location in Silverlight Windows Phone 7 emulator
Continuous GPS tracking drains the battery..
Is there any way I can schedule the app to get the location or maybe keep the app running in the background to save the battery ?
6/2/2011 1:53 AM | # re: Simulate Geo Location in Silverlight Windows Phone 7 emulator
It is wonderful
7/21/2011 2:26 AM | # re: Simulate Geo Location in Silverlight Windows Phone 7 emulator
This is the type of article that I’ve been interested to read. Because it is really helps me in set up this application to my Windows Phone 7.
4/19/2012 4:39 AM | # re: Simulate Geo Location in Silverlight Windows Phone 7 emulator
I am huge windows phone fan and I recently started to build some apps for this platform. So far I have only some crappy things done but I am still motivated to learn how to develop apps for mobile phones
7/1/2012 6:27 AM | # re: Simulate Geo Location in Silverlight Windows Phone 7 emulator
vrey nice ...
8/28/2012 7:39 PM | # re: Simulate Geo Location in Silverlight Windows Phone 7 emulator
Current GPS (2012) have used this more. It is much more convenient to access it.

 
Please add 2 and 6 and type the answer here:

DISCLAIMER:

The opinions/content expressed on this blog are provided "ASIS" with no warranties and are my own personal opinions/content (unless otherwise noted) and do not represent my employer's view in any way.