Friday, November 11, 2011

Location Services : Simulating Locations

This series discusses the iPhone Location Services.  In this post, we learn how to change the default iOS Simulator location and simulate other locations.

Default Location
In the last post, we got the LSDemo app up and running and saw a stream of updates like this in the Xcode console:
2011-11-10 11:44:15.453 LSDemo[20964:f803] newLocation=<+37.78583400,-122.40641700> +/- 5.00m (speed -1.00 mps / course -1.00) @ 11/10/11 11:44:15 AM Pacific Daylight Time
2011-11-10 11:44:15.456 LSDemo[20964:f803] oldLocation=<+37.78583400,-122.40641700> +/- 5.00m (speed -1.00 mps / course -1.00) @ 11/10/11 11:44:14 AM Pacific Daylight Time
Notice the location each time is <+37.78583400,-122.40641700>.  A simple Google Maps query reveals this corresponds to the Apple store in San Francisco:
Why is the iOS Simulator using this location, and how can we change it?  

To understand where this is coming from, launch LSDemo in the iOS Simulator.  Open the Debug > Location menu and notice that "Custom Location" is checked:
Click on it to reveal the custom location:
Yay!  Now we know why LSDemo keeps reporting a location of <+37.78583400,-122.40641700>.  This is the default custom location that comes with Xcode.

Simulating Locations
What if we would like to simulate another location?  At first glance, the choices provided by the iOS Simulator  Debug > Location menu appear limited:
To simulate a more exotic location, we could manually look up the GPS coordinates, and then paste them each time into the "Custom Location" dialog.  However, this can be tedious.  Fortunately Xcode offers another way to simulate locations.  While the LSDemo is running in iOS Simulator, click the GPS arrow icon (circled in red):
This will reveal a list of pre-programmed alternatives:
For fun, let's select Sydney, Australia.  As the LSDemo app is running in the iOS Simulator, the new location should be reflected in the console log:
2011-11-10 11:52:08.365 LSDemo[21129:f803] oldLocation=<+37.78583400,-122.40641700> +/- 5.00m (speed -1.00 mps / course -1.00) @ 11/10/11 11:52:07 AM Pacific Daylight Time
2011-11-10 11:52:09.056 LSDemo[21129:f803] newLocation=<-33.86340000,+151.21100000> +/- 5.00m (speed -1.00 mps / course -1.00) @ 11/10/11 11:52:09 AM Pacific Daylight Time
Notice how the location changed, from <+37.78583400,-122.40641700>  to  <-33.86340000,+151.21100000>.  (Color added for emphasis.)  A simple Google Maps query reveals this is indeed Sydney, Australia:
Yay!  We are receiving the new location updates as we expected.  

Depending on the application, a constant stream of updates may be useful.  However, if we just want a location fix, as we will for HelloWorld, the continuous updates will be overkill.  How do we stop the updates once we have our location?  We shall learn that in the next post.