2011-09-29 49 views
-1

如何僅在按下按鈕時才能更新位置?如何僅在按下按鈕時更新位置

我有一個名爲「REFRESH」的按鈕。每次按下此按鈕時,我都想向用戶顯示他們的位置。例如,51 Bourke Street, Victoria

但是,我不想定期更新我的位置。我只想在按下按鈕時更新其位置,以節省電池電量。

您認爲如何?我做得對嗎?

我有這些類:

  • VoteViewController.h和VoteViewController.m
  • CoreLocationController.h和CoreLocationController.m

這是我有:

VoteViewController.h

@interface VoteViewController : UIViewController <CoreLocationControllerDelegate> 
{ 
    CoreLocationController *coreController; 
} 

- (void)locationUpdate:(CLLocation *)location; 
- (void)locationError:(NSError *)error; 
- (void)geoReverseAddress:(MKPlacemark *)placeMark; 
- (IBAction)refreshButtonPressed; 

VoteViewController.m

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    coreController = [[CoreLocationController alloc] init]; 
    coreController.delegate = self; 
} 

- (IBAction)refreshButtonPressed 
{ 
    NSLog(@"Refresh Button pressed"); 
    label.text = [NSString stringWithString:@""]; 
    [coreController.locationManager startUpdatingLocation]; 
} 

- (void)locationUpdate:(CLLocation *)location 
{ 
    comments.text = [location description]; 
    [coreController.locationManager stopUpdatingLocation]; 
} 

- (void)locationError:(NSError *)error 
{ 
    comments.text = [error description]; 
    [coreController.locationManager stopUpdatingLocation]; 
} 

- (void)geoReverseAddress:(MKPlacemark *)placeMark 
{ 
    label.text = [NSString stringWithFormat:@"%@ %@, %@", [placeMark subThoroughfare], 
[placeMark thoroughfare], [placeMark locality]]; 
} 

CoreLocationController.h

@protocol CoreLocationControllerDelegate <NSObject> 

@required 
- (void)locationUpdate:(CLLocation *)location; 
- (void)locationError:(NSError *)error; 
- (void)geoReverseAddress:(MKPlacemark *)placeMark; 

@end 

@interface CoreLocationController : NSObject <CLLocationManagerDelegate, MKReverseGeocoderDelegate> 
{ 
    CLLocationManager *locationManager; 
    id delegate; 

    MKReverseGeocoder *reverse; 
} 

@property(nonatomic, retain) CLLocationManager *locationManager; 
@property(nonatomic, retain) id delegate; 
@end 

CoreLocationController.m

-(id) init 
{ 
    self = [super init]; 

    if (self != nil) 
    { 
     self.locationManager = [[[CLLocationManager alloc] init] autorelease]; 
     self.locationManager.delegate = self; 
     self.locationManager.distanceFilter = kCLHeadingFilterNone; 
     self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    } 
    return self; 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    NSLog(@"Update location"); 
    [self.delegate locationUpdate:newLocation]; 

    reverse = [[MKReverseGeocoder alloc] initWithCoordinate:[newLocation coordinate]]; 
    reverse.delegate = self; 
    [reverse start]; 
} 

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{ 
    [self.delegate locationError:error]; 
} 

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error 
{ 
    [self.delegate locationError:error]; 
    [reverse cancel]; 
    [reverse release]; 
} 

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark 
{ 
    [self.delegate geoReverseAddress:placemark]; 
    [reverse cancel]; 
    [reverse release]; 
} 

回答

0

當你第一次啓動CLLocationManager時,你很可能會從上次運行時得到一個陳舊的位置。一旦出現這種情況,當設備使用WiFi嗅探和單元三角測量時,您將開始獲取非常不準確的位置,而GPS則尋找修復方法。

因此,在您的didUpdateToLocation方法中,您可能想要丟棄第一個匹配項,然後測試newLocation對象的.horizo​​ntalAccuracy值,以獲取足夠低的值來信任它。

除此之外,我沒有看到你在這裏發送的東西有什麼不好。我不確定我會在自己的類中包裝位置獲取工作的麻煩,我可能會在我的viewController中執行此操作。但這是一種風格選擇。如果你在其他地方重複使用這個功能,你在這裏得到的東西顯然是要走的路。

+0

丹雷伊,我的代碼的問題是,它運作時,我第一次運行它(說我的房子)..但是,當我嘗試運行它在另一個地方(說我的朋友的房子)..它確實不行。 :(它仍然會顯示我的舊位置(這是我的房子在這種情況下) 希望我自己清楚 –

+0

這很有道理,返回的第一個位置通常是上次獲取位置時的緩存位置。這就是爲什麼它會顯示你仍然在你的房子裏,例如你可以檢查收到的位置的時間戳,並拋出超過3分鐘的任何東西,例如(我相信這是蘋果在他們的文檔中使用的時間長度)。 – superjessi