【问题标题】:Pass Coordinates from locationManager in appDelegate to viewController将 appDelegate 中 locationManager 的坐标传递给 viewController
【发布时间】:2012-12-20 05:17:08
【问题描述】:

我试图在 viewController 出现时获取用户的坐标。我需要几个viewController中的位置,所以我把locationManager放在appDelegate中。我的问题是,在第一个 viewDidAppear 上,坐标还没有找到。我应该如何去改变这个?谢谢大家!!!

我的 AppDelegate 有这个:

- (NSString *)getUserCoordinates
{
NSString *userCoordinates = [NSString stringWithFormat:@"latitude: %f longitude: %f", 
locationManager.location.coordinate.latitude,     locationManager.location.coordinate.longitude];
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
return userCoordinates;
}

我的 viewController 用这个获取坐标:

- (void)viewDidAppear 
{
NSString *userCoordinates =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate 
getUserCoordinates];
}

【问题讨论】:

    标签: iphone ios xcode core-location locationmanager


    【解决方案1】:

    我最近在 AppDelegate 中实现了相同的位置管理器,以便我的 ViewController 都可以访问位置数据。

    不要忘记实现 CoreLocation 委托方法,尤其是 didUpdateLocations 以获取新的位置数据。我会把它放在 AppDelegate 类中。

    由于您有一个对象(AppDelegate)需要通知许多 viewController 位置更新的关系,因此我建议使用 NSNotification。

    在你的 AppDelegate 中,你会写...

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    // set up location manager
    self.locationManager = [[CLLocationManager alloc] init];
    [self.locationManager setDelegate:self];
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [self.locationManager startUpdatingLocation];
    return YES;
    }
    
    - (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations {
    CLLocation * newLocation = [locations lastObject];
    // post notification that a new location has been found
    [[NSNotificationCenter defaultCenter] postNotificationName:@"newLocationNotif"
                                                                object:self
                                                              userInfo:[NSDictionary dictionaryWithObject:newLocation
                                                                                                   forKey:@"newLocationResult"]];
    }
    

    在您的 ViewController 中,您不会使用 viewDidAppear 方法。相反,你会这样做......

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    // subscribe to location updates
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(updatedLocation:)
                                                 name:@"newLocationNotif"
                                               object:nil];
    }
    

    你会有一个方法 updatedLocation 看起来像这样

    -(void) updatedLocation:(NSNotification*)notif {
    CLLocation* userLocation = (CLLocation*)[[notif userInfo] valueForKey:@"newLocationResult"];
    }
    

    您可以通过将其他 viewController 添加为观察者来订阅通知更新。

    【讨论】:

    • 效果很好!谢谢你。但是,用户的初始位置将丢失,因为在通知已经发送后会加载监听视图。我在 AppDelegate 中添加了变量以保留所有视图访问的位置。
    • @thedp 你是怎么做到的?您能否展示可从所有视图访问该变量的示例?
    • @UtkuDalmaz 我已经在 AppDelegate 中放置了一个 CLLocation 变量,并且从其他视图控制器中你可以使用这个 appDelegateInst = (AppDelegate *)[[UIApplication sharedApplication] delegate];。现在您可以访问所有 appDelegate 变量,例如全局变量。
    【解决方案2】:

    您还可以做的是将CLLocation保存为带有纬度和经度的字典到keypath @“currentUserLocation”下的userDefaults(只是一个命名示例)。

    在您的viewDidLoad 中,您将控制器设置为键路径的观察者,如下所示:

    [[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:@"currentUserLocation" options:NSKeyValueObservingOptionNew context:NULL];
    

    并实现类似:

    -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if ([keyPath isEqualToString:@"currentUserLocation"]) {
    
        NSUserDefaults *df = [NSUserDefaults standardUserDefaults];
        NSDictionary *dict = [df objectForKey:@"currentUserLocation"];
        NSLog(@"yea we have updated location dict %@", dict);
    }
    

    }

    这意味着您可以在应用程序中的任何位置更新您的用户位置,并且只要您更新 UserDefault(在我的情况下为 keyPath @"currentUserLocation"),任何观察者都会得到这个。我想它在某种程度上有点像通知中心? :)

    这个解决方案非常适合我的情况。

    【讨论】:

      猜你喜欢
      • 2010-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-17
      • 2015-04-03
      • 1970-01-01
      相关资源
      最近更新 更多