Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Welcome to the iOS community!</p> <p>First, you'll probably find it helpful to look at the <a href="https://developer.apple.com/Library/ios/documentation/CoreLocation/Reference/CLLocationManagerDelegate_Protocol/CLLocationManagerDelegate/CLLocationManagerDelegate.html#jumpTo_12" rel="nofollow">CLLocationManagerDelegate</a> reference, under the heading for the <code>locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations</code> method. The important bit here is:</p> <blockquote> <p>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.</p> </blockquote> <p>The important thing to get is that this method is called by the system when the user's location changes.</p> <p>So, for instance, you could print out a message to your console when the location updates by changing your implementation to something like:</p> <pre><code>-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { CLLocation *currentLocation = [locations lastObject]; NSLog(@"Location is: %.5f %.5f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude); } </code></pre> <p>If you instead want to do something in response to user activity, you could use <code>manager.location</code>, 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:.)</p> <p>So, for instance, if you wanted to update your label with the current location whenever the button was pressed, you could add something like:</p> <pre><code>-(IBAction)gpsButton { CLLocation *currentLocation = self.gpsLM.location; self.gpsLabel.text = [NSString stringWithFormat:@"Location is: %.5f, %.5f"]; } </code></pre> <p>(Note: this assumes that the gpsButton action and the gpsLabel outlet are hooked up to something graphically in Interface Builder.)</p> <p>If you're familiar with other programming languages, this is a push versus pull distinction. CLLocationManager provides a <em>push</em> model (it calls your <code>locationManager:didUpdateLocations:</code> method to inform you immediately of changes), and also a <em>pull</em> model (you can ask it for the most current location through <code>.location</code> at any time). Which one you use will depend on what you want to do.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload