如何仅在按下按钮时更新位置

时间:2011-09-29 14:00:41

标签: iphone ios xcode core-location

如何仅在按下按钮时才能使应用程序更新位置?

我有一个名为“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];
}

1 个答案:

答案 0 :(得分:0)

当您第一次启动CLLocationManager时,您很可能从上次运行时获得一个陈旧位置。一旦完成,当设备使用WiFi嗅探和小区三角测量时,你将开始获得非常不准确的位置,而GPS则寻找修复。

因此,在您的didUpdateToLocation方法中,您可能希望丢弃第一个匹配项,然后测试newLocation对象的.horizo​​ntalAccuracy值,以获得足够低的信任值。

除此之外,我没有看到你发送的内容有什么不好。我不确定我是否会在自己的类中包装位置获取工作的麻烦,我可能只是在我的viewController中执行此操作。但这是一种风格选择。如果您在其他地方重复使用此功能,那么您在这里所拥有的内容显然就是您的选择。

相关问题