Pergunta

My app display the user's last known/current coordinates in a text label when a button is pressed.

I set up a block of code in my main file to print the user's latitude coordinate to the log, but it does not print anything to the log when I run the app.

Why won't the NSLog print to the console?

Here is the snippet of code that is supposed to be printing the location to the log once the app launches and the user allows the app to access their location:

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

CLLocation * currentLocation = [locations lastObject];

NSLog(@"%f", currentLocation.coordinate.latitude);

Below is my full ViewController.h code:

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>


@interface ViewController : UIViewController <CLLocationManagerDelegate>

@property (nonatomic, strong) IBOutlet UILabel * gpsLabel;
@property (nonatomic, strong) CLLocationManager * gpsLM;

-(IBAction)gpsButton;

@end

And here is my full ViewController.m code:

#import "ViewController.h" //This imports the all of the code we have typed in the     ViewController.h file.
#import <CoreLocation/CoreLocation.h> //This imports the CoreLocation framework needed for    location apps.


//This assigns the Location Manager's delegate to this view controller

@interface ViewController () <CLLocationManagerDelegate>

//This tells the delegate that new location data is available. Manager is the object that updates the event, and the locations object is where the array of location data is stored.

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray    *)locations;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    //This allocates memory for and initializes the gpsLM object we setup in ViewController.h
    //This means that we can now use the object and do things with it.

    self.gpsLM = [[CLLocationManager alloc]init];

    //This calls a startUpdatingLocation method for our CLLocationManager object called gpsLM.
    //Because this is all in viewDidLoad, it all gets executed right away as soon as the app    is opened.

    [self.gpsLM startUpdatingLocation];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//This executes the instance method that we declared above in the header.
//Now we are actually implementing the method and can tell it what we want it to do.

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

    //This creates an object called currentLocation and sets it's value to whatever the last  value is in the locations array.
    //Notice how it is also calling a method of lastObject for the object called locations.
    //So remember that you can set variables and objects equal to the result of a method call.

    CLLocation * currentLocation = [locations lastObject];

    //This prints out text to the debug console that states the latitude coordinate of the user's iPhone.

    NSLog(@"%f", currentLocation.coordinate.latitude);

}


-(IBAction)gpsButton{

    CLLocation * currentLocation = self.gpsLM.location;

    self.gpsLabel.text = [NSString stringWithFormat:@"Your Location is %@", currentLocation];

}

@end
Foi útil?

Solução

It seems that you forgot to assign the location manager delegate:

self.gpsLM = [[CLLocationManager alloc]init];
self.gpsLM.delegate = self; // <-- ADD THIS
[self.gpsLM startUpdatingLocation];

Without this assignment, the location manager doesn't know what object to give the location update to. The method locationManager:didUpdateLocations: never runs.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top