2013-02-13 84 views
1

我試圖實現一個單獨的類來管理我的位置。每當我點擊一個按鈕時,我都想獲得我的位置。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委託方法根本沒有被調用。

+0

Upvoted,因爲原因有點隱藏,你現在有足夠的信譽來upvote正確的答案。 – AlexWien 2013-02-13 17:00:20

回答

2

代表方法必須命名爲locationManager:didUpdateToLocation:fromLocation:locationManager:didFailWithError:

不能使用自定義方法名稱,如gpsManager:didUpdateToLocation:fromLocation:


另請注意,自iOS 6起棄用locationManager:didUpdateToLocation:fromLocation:,應使用locationManager:didUpdateLocations:

+0

啊!謝謝!!有效。 – catchmrbharath 2013-02-13 16:51:00

2

您didUpdateToLocation的簽名是錯誤的: 這是我的代碼:

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

而且設置desiredAccuracyCLLocationAccuracyBest

+0

我在Xcode上模擬它。所以這似乎沒有問題 – catchmrbharath 2013-02-13 16:40:50

+0

我設置它,但它不工作。更新方法根本沒有被調用。 updateLocation中的NSLog消息沒有打印。 – catchmrbharath 2013-02-13 16:47:00

+0

發現它,看到更新回答 – AlexWien 2013-02-13 16:50:03