Coordinates Using the iPhone Simulator
One of the small challenges in developing iPhone applications is using the GPS location in the iPhone Simulator. While it’s good to test frequently on the iPhone during development, the Simulator is used most often and it’s difficult to recreate what you are seeing on the iPhone when your iPhone properly determines where you are at, but the Simulator defaults to Apple’s office at 1 Infinite Loop in California.
I’ve gone in from time to time to hardcode results in the simulator to be my current location so that the Simulator will return the same coordinates as the iPhone, but this can be dangerous because you have to remember to take out the hard coded coordinates.
By using the UIDevice class, you can easily determine if you are using the Simulator or not. By determining if you are using the Simulator, you can throw in your test coordinates and not worry about accidentally leaving in test coordinates when you are making a build for users.
The code example below shows a locationManager function to do a onetime retrieval of the position, detect if you are using the Simulator and then set default coordinates:
CLLocationCoordinate2D loc = [newLocation coordinate];
NSLog( @"We're at %f, %f\n", loc.latitude, loc.longitude );
//get a string that contains what the current device is: iPod, iPod Touch, etc.
NSString* model = [[UIDevice currentDevice] model];
if ( [model compare:@"iPhone Simulator"] == NSOrderedSame )
NSLog( @"We're using the simulator so override the coordinates" );
myCoordinate.latitude = 41.9030592;
myCoordinate.longitude = -87.9871786;
myCoordinate.latitude = loc.latitude;
myCoordinate.longitude = loc.longitude;
//user made function to update the screen with the results from myCoordinate
This mechanism is also good for testing your application in places you’ll never get to. It’s not good practice to only test a location based app in the small range of places you might be doing your development.
Have other ideas? Contact us with them.
- Dave Gruen Senior Vice-President, Consulting Division