我試圖實現一個單獨的類來管理我的位置。每當我點擊一個按鈕時,我都想獲得我的位置。CoreLocation經理沒有更新位置
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委託方法根本沒有被調用。
Upvoted,因爲原因有點隱藏,你現在有足夠的信譽來upvote正確的答案。 – AlexWien 2013-02-13 17:00:20