CoreLocation管理器不更新位置

时间:2013-02-13 16:33:38

标签: ios objective-c gps core-location

我正在尝试实施一个单独的类来管理我的位置。每当我点击一个按钮时,我想获取我的位置。

gpsFilter.h

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

@interface gpsFilter : NSObject <CLLocationManagerDelegate>

@property (nonatomic, retain) CLLocationManager *gpsManager;
@property (nonatomic, retain) NSString * latitude;
@property (nonatomic, retain) NSString * longitude;
@end

gpsFilter.m

#import "gpsFilter.h"

@implementation gpsFilter

- (id) init{
self = [super init];
if(self != nil){
    self.gpsManager = [[CLLocationManager alloc] init];
    self.gpsManager.delegate = self;
    [self.gpsManager startUpdatingLocation];
    BOOL enable = [CLLocationManager locationServicesEnabled];
    NSLog(@"%@", enable? @"Enabled" : @"Not Enabled");
}
return self;
}

- (void)gpsManager:(CLLocationManager *) manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if(currentLocation != nil){
    self.latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
    self.longitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}
}

- (void)gpsManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
}
@end

我正在使用一个单独的类,因为我希望将来添加平滑过滤器。我没有得到任何更新。 NSLog永远不会被触发。我认为一些变量是自动释放的,但我不确定,哪一个。

viewController代码如下。

#import "gpstestViewController.h"

@interface gpstestViewController (){

}

@end

@implementation gpstestViewController


- (void)viewDidLoad
 {
[super viewDidLoad];
self.location = [[gpsFilter alloc] init];
// Do any additional setup after loading the view, typically from a nib.
}


- (IBAction)getloca:(id)sender {
self.latitudeLabel.text = [self.location latitude];
self.longitudeLabel.text = [self.location longitude];

}

- (IBAction)getLocation:(id)sender {
self.latitudeLabel.text = [self.location latitude];
self.longitudeLabel.text = [self.location longitude];

}
@end

我很抱歉我倾倒了很多代码,但我是ios编程的新手,我无法找到问题所在。

编辑:根本没有调用updateLocation委托方法。

2 个答案:

答案 0 :(得分:2)

您的didUpdateToLocation的签名是错误的: 这是我的代码:

/** Delegate method from the CLLocationManagerDelegate protocol. */
- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation 
           fromLocation:(CLLocation *)oldLocation    
{
  // here do 
}

进一步将desiredAccuracy设置为CLLocationAccuracyBest

答案 1 :(得分:2)

委托方法必须命名为locationManager:didUpdateToLocation:fromLocation:locationManager:didFailWithError:

您无法使用gpsManager:didUpdateToLocation:fromLocation:等自定义方法名称。


另请注意,自iOS 6起,locationManager:didUpdateToLocation:fromLocation:已弃用,您应使用locationManager:didUpdateLocations: