5

当 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];
}
4

2 回答 2

7

我最近在 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"];
}

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

于 2013-01-06T07:09:45.227 回答
0

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

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

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

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

于 2014-02-12T09:12:37.317 回答