Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. COThank you! This worked perfectly. I do have 2 more quick questions for you though. When using your 2nd block of code to send the coordinates to the label via a touch on gpsButton it worked perfectly, but I could not get your first block of code print out the coordinates in the console. I basically just pasted that code in and ran the app and then allowed it to view my location. Shouldn't it print to the console as soon as I do that?
      singulars
    2. CO2nd question: The gpsButton action was not working for me several times. So I went into my header file and main file and changed my action from -(IBAction)gpsButton; to -(IBaction)gpsButton:(id)sender and then deleted the old connection and re-connected the button to the new action with (id)sender. Then the app was working correctly. However, I just went back and deleted the (id)sender connection and retried the old connection and now it is working! So which one is correct. If I don't need (id)sender, then can you explain why please? Thank you very much for the help.
      singulars
    3. COGreat! I'm glad it's helped. As to the log output not showing up: well, my initial description was rather inaccurate - it should show up in the debug console in Xcode itself, not the separate Console app - so make sure you are looking there. Also make sure your debug console is visible - if you're using Xcode 5, there will be two rectangular icons in the lower right corner of the window. Make sure both are enabled (blue). For the connection: either will work. My guess as to why it didn't work the first time would be that you may have replaced the button after connecting it originally.
      singulars
 

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