Welcome to the iOS community!
First, you'll probably find it helpful to look at the CLLocationManagerDelegate reference, under the heading for the locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
method. The important bit here is:
locations: An array of CLLocation objects containing the location data. This array always contains at least one object representing the current location. If updates were deferred or if multiple locations arrived before they could be delivered, the array may contain additional entries. The objects in the array are organized in the order in which they occurred. Therefore, the most recent location update is at the end of the array.
The important thing to get is that this method is called by the system when the user's location changes.
So, for instance, you could print out a message to your console when the location updates by changing your implementation to something like:
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *currentLocation = [locations lastObject];
NSLog(@"Location is: %.5f %.5f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
}
If you instead want to do something in response to user activity, you could use manager.location
, which is automatically updated every time the CLLocationManager detects a new location. (If this didn't exist, you would need to add another instance variable to your class to store the most recent location, and update that in locationManager:didUpdateLocations:.)
So, for instance, if you wanted to update your label with the current location whenever the button was pressed, you could add something like:
-(IBAction)gpsButton {
CLLocation *currentLocation = self.gpsLM.location;
self.gpsLabel.text = [NSString stringWithFormat:@"Location is: %.5f, %.5f"];
}
(Note: this assumes that the gpsButton action and the gpsLabel outlet are hooked up to something graphically in Interface Builder.)
If you're familiar with other programming languages, this is a push versus pull distinction. CLLocationManager provides a push model (it calls your locationManager:didUpdateLocations:
method to inform you immediately of changes), and also a pull model (you can ask it for the most current location through .location
at any time). Which one you use will depend on what you want to do.