如何仅在按下按钮时才能更新位置?如何仅在按下按钮时更新位置
我有一个名为“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