访问用户的位置一旦进入viewDidAppear方法

时间:2013-01-16 09:59:22

标签: iphone ios objective-c core-location locationmanager

我有两个不同的UIViewControllers,需要访问用户的LatitudeLongitude。我只需要在视图出现时访问该位置。我目前正在将LocationManager存储在应用委托中,但viewDidAppear:方法加载速度太快,无法找到用户的位置。有谁知道最好的方法吗?谢谢!

2 个答案:

答案 0 :(得分:1)

我创建了一个共享LocationManager。收到回电后,请startUpdatingLocationstopUpdatingLocation

以下是我使用的代码:

appDelegate添加以下方法:

+ (NHAppDelegate *)sharedDelegate
{
    return (NHAppDelegate *)[[UIApplication sharedApplication] delegate];
}

使LocationManager可用:

@property (nonatomic, strong) CLLocationManager *locationManager;

然后在你的UIViewController中你可以这样做:

[NHAppDelegate sharedDelegate].locationManager.delegate = self;
[[NHAppDelegate sharedDelegate].locationManager startUpdatingLocation];

然后我使用以下代码获取位置并将其显示在UILabel中。

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    [self loadPositionWithLocation:newLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *location = [locations objectAtIndex:0];
    [self loadPositionWithLocation:location];
}

-(void)loadPositionWithLocation:(CLLocation *)newLocation
{
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:newLocation completionHandler:
     ^(NSArray* placemarks, NSError* error){
         if ([placemarks count] > 0)
         {
             CLPlacemark *placemark = [placemarks objectAtIndex:0];

             NSString *thoroughfare = placemark.thoroughfare;
             NSString *subThoroughfare = placemark.subThoroughfare;
             NSString *postalCode = placemark.postalCode;
             NSString *locality = placemark.locality;

             if( thoroughfare == nil )      {  thoroughfare = @"";     }
             if( subThoroughfare == nil )   {  subThoroughfare = @"";  }
             if( postalCode == nil )        {  postalCode = @"";       }
             if( locality == nil )          {  locality = @"";         }

             NSString *addressString = [NSString stringWithFormat:@"%@ %@\n%@ %@", thoroughfare, subThoroughfare, postalCode, locality];

             self.positionLabel.text = addressString;

             [[NHAppDelegate sharedDelegate].locationManager stopUpdatingLocation];
         }
     }];
}

重要的是最后一行stopUpdatingLocation,因此LocationManager只会被调用一次。

答案 1 :(得分:1)

听起来像NSNotification有用的典型案例。我相信CoreLocation不会发送通知。在这种情况下,你必须自己做。在AppDelegate.h中,您可以定义:

const string* LocationUpdateNotification  = @"LocationUpdateNotification";

然后在AppDelegate.m中的CoreLocation委托方法中执行:

dispatch_async(dispatch_get_main_queue(), ^
{
    [[NSNotificationCenter defaultCenter] postNofificationName:LocationUpdateNotification 
                                                        object:self];
});

最后在每个视图控制器中:

    [[NSNotificationCenter defaultCenter] addObserverForName:MyNotification
                                                      object:nil
                                                       queue:[NSOperationQueue mainQueue]
                                                  usingBlock:^(NSNotification* note)
    {
        // YOUR LOCATION UPDATE CODE.
    }];