2013-11-21 42 views
0

A ViewController启动了位置跟踪。我想NSLog的位置更新(当模拟器在高速公路驱动器上),但didUpdateLocations只有当我注销NSTimer时记录,我不明白为什么。我对此感到非常愚蠢,这可能很简单,我对此很陌生,并花了很长时间试图解决这个问题。无法让我的位置更新

ViewController: 

#import "ViewController.h" 

@interface ViewController() 


@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    if ([defaults boolForKey:@"locationUpdate"]) { 
     LocationTracking *locationTracker = [[LocationTracking alloc] init]; 
     [locationTracker startTracking]; 
     NSLog(@"location tracking did"); 
     //NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10 target:locationTracker selector:@selector(logLast) userInfo:nil repeats:YES]; 

    } 

} 

LocationTracking.h:

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

@interface LocationTracking : NSObject <CLLocationManagerDelegate> 
@property (nonatomic, strong) NSMutableArray *locations; 
-(void) startTracking; 
-(void) logLast; 

@end 

LocationTracking.m:

#import "LocationTracking.h" 

@interface LocationTracking() 
@property (nonatomic, strong) CLLocationManager *locationManager; 

@end 

@implementation LocationTracking 

-(void)startTracking 
{ 
    self.locations = [[NSMutableArray alloc] init]; 
    self.locationManager = [[CLLocationManager alloc] init]; 
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    self.locationManager.distanceFilter = kCLDistanceFilterNone; 
    [self.locationManager setDelegate:self]; 
    [self.locationManager startUpdatingLocation]; 

} 

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
{ 
    NSLog(@"did update %@",[locations lastObject]); 
} 

-(void)logLast 
{ 

} 
+0

didUpateLocations被调用一次或没有? –

+0

当定时器触发(未注释掉)时,每次更新位置时,didUpdateLocations都会更新并记录日志。但是当计时器被注释掉时,它不会记录,所以我不认为它会被触发或被调用。 – amk26cap

回答

0

下面的代码工作正常,我:

#import "ViewController.h" 
@import CoreLocation; 

@interface ViewController() <CLLocationManagerDelegate> 

@property (strong,nonatomic) CLLocationManager *locationManager; 

@end 

@implementation ViewController 

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    self.locationManager = [[CLLocationManager alloc] init]; 
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    self.locationManager.distanceFilter = kCLDistanceFilterNone; 
    [self.locationManager setDelegate:self]; 
    [self.locationManager startUpdatingLocation]; 

} 

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

    CLLocation *loc = [locations lastObject]; 
    NSString *lat = [NSString stringWithFormat:@"%.2f",loc.coordinate.latitude]; 
    NSString *lon = [NSString stringWithFormat:@"%.2f",loc.coordinate.longitude]; 

    NSLog(@"location is: %@, %@",lat,lon); 
} 


@end 

如果您使用的是Xcode模拟器,不要忘了设置模拟器的位置。新项目默认为“无”。

+0

我实际上并不想使用的NSTimer可言,但现在它只是如果我有(调用一个空方法)的作品,我希望didUpateLocations方法产生的日志。将其更改为自行停止所有日志记录,与注释相同。 – amk26cap

+0

你实际上'#import“LocationTracking.h”'到你的'ViewController.m'中吗?我没有看到你的示例代码。 – wigging

+0

@Gavin如果他没有在ViewController中包含“LocationTracking.h”。代码如何编译? –