如何僅在按下按鈕時才能更新位置?如何僅在按下按鈕時更新位置
我有一個名爲「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];
}
丹雷伊,我的代碼的問題是,它運作時,我第一次運行它(說我的房子)..但是,當我嘗試運行它在另一個地方(說我的朋友的房子)..它確實不行。 :(它仍然會顯示我的舊位置(這是我的房子在這種情況下) 希望我自己清楚 –
這很有道理,返回的第一個位置通常是上次獲取位置時的緩存位置。這就是爲什麼它會顯示你仍然在你的房子裏,例如你可以檢查收到的位置的時間戳,並拋出超過3分鐘的任何東西,例如(我相信這是蘋果在他們的文檔中使用的時間長度)。 – superjessi