2011-09-29 48 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