从appDelegate中的locationManager传递坐标到viewController

时间:2013-01-06 05:38:10

标签: iphone ios xcode core-location locationmanager

我试图在viewController出现时获取用户的坐标。我需要几个viewControllers中的位置,所以我将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];
}

2 个答案:

答案 0 :(得分:6)

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

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

因为,你有一个关系,一个对象(AppDelegate)需要通知许多viewControllers有关位置更新,我建议使用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"];
}

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

答案 1 :(得分:0)

你还可以做的是将CLLocation作为具有纬度和经度的字典保存到keypath @“currentUserLocation”下的userDefaults(只是一个namingexample)。

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”),任何观察者都会得到此信息。我想它在某种程度上有点像通知中心? :)

此解决方案适用于我的案例。