【问题标题】:Access User's Location Once in viewDidAppear Method在 viewDidAppear 方法中访问用户的位置
【发布时间】:2012-12-30 14:37:23
【问题描述】:

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

【问题讨论】:

  • ViewDidLoad方法中编写你的代码,它只会加载一次。
  • 谢谢,但它仍然不够快。在找到位置之前显示/加载视图

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


【解决方案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 只被调用一次。

【讨论】:

    【解决方案2】:

    听起来像是NSNotifications 很有用的典型案例。我相信 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.
        }];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-29
      • 1970-01-01
      • 1970-01-01
      • 2018-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多