0

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

4

2 回答 2

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只有一次被调用。

于 2013-01-16T10:28:10.520 回答
1

NSNotification听起来像是s 有用的典型案例。我相信 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.
    }];
于 2013-01-16T10:35:25.763 回答