使用UIButton的CLLocationManager startUpdatingLocation

Nee*_*esh 1 iphone xcode objective-c cllocationmanager

这是我第一次使用CLLocationManager.我不确定我做的是否正确.如果我错了,请纠正我.

我在我的viewDidLoad方法中初始化locationManager 并以相同的方法告诉locationManager startUpdatingLocation.

当代表收到时

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{

    [locationManager stopUpdatingLocation];
    //do stuff with the coordinates
}
Run Code Online (Sandbox Code Playgroud)

避免重复调用此委托方法.

我有一个UIButton用户可以点击更新位置的地方

//called by user action
-(IBAction)updateLocation{
    //start updating delegate
    locationManager.delegate=self;
    [locationManager startUpdatingLocation];    
}
Run Code Online (Sandbox Code Playgroud)

但是当位置发生变化时,当我点击时UIbutton,位置坐标根本没有变化.:(

我究竟做错了什么?这是做的权利还是我根本不应该停止locationManager?

帮助将不胜感激.

Gar*_*ies 10

CLLocationManager缓存您的上一个位置,并在您打电话后立即返回-startUpdatingLocation.因此,您正在开始更新,接收旧位置,然后停止更新.

这不是如何-startUpdatingLocation/-stopUpdatingLocation使用的.正如我上面提到的,多次调用委托方法有什么问题?如果你只想要当用户点击一个按钮,得到的位置,离开了CLLocationManager更新,并且只检查CLLocationMangerlocation财产当用户点击您的按钮.

如果您试图避免多次调用委托方法的原因是因为您担心功耗等,请使用以下内容调整其desiredAccuracy属性CLLocationManager:locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters.

总而言之,它可能看起来像这样......

.h文件:

@interface YourController : NSObject <CLLocationManagerDelegate>

@property (nonatomic, retain) CLLocationManager *locationMgr;
@property (nonatomic, retain) CLLocation *lastLocation;

- (IBAction)getNewLocation:(id)sender;

@end
Run Code Online (Sandbox Code Playgroud)

.m文件:

@interface YourController

@synthesize locationMgr = _locationMgr;
@synthesize lastLocation = _lastLocation;

- (id)init
{
    if (self = [super init]) {
        self.locationMgr = [[CLLocationManager alloc] init];
        self.locationMgr.desiredAccuracy = kCLLocationAccuracyBest;
        self.locationMgr.delegate = self;
    }
    return self;
}

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
           fromLocation:(CLLocation *)oldLocation
{
    if (!self.lastLocation) {
        self.lastLocation = newLocation;        
    }

    if (newLocation.coordinate.latitude != self.lastLocation.coordinate.latitude &&
        newLocation.coordinate.longitude != self.lastLocation.coordinate.longitude) {        
        self.lastLocation = newLocation;
        NSLog(@"New location: %f, %f",
              self.lastLocation.coordinate.latitude,
              self.lastLocation.coordinate.longitude);
        [self.locationMgr stopUpdatingLocation];
    }
}

- (IBAction)getNewLocation:(id)sender
{
    [self.locationMgr startUpdatingLocation];
    NSLog(@"Old location: %f, %f", 
          self.lastLocation.coordinate.latitude, 
          self.lastLocation.coordinate.longitude);
}

- (void)dealloc
{
    [self.locationMgr release];
    self.locationMgr = nil;
    [self.lastLocation release];
    self.lastLocation = nil;
    [super dealloc];
}

@end
Run Code Online (Sandbox Code Playgroud)