我在我的应用程序delegate
中调用locationManager
,并引用各种viewDidAppear
方法中的getUserLatitude和getUserLongitude函数 View Controller
。
我的问题是,我的 viewDidAppear
方法启动太快,appDelegate
无法定位用户,并且我得到的 latitude
为 0.000000,并且0.000000 表示经度
。有谁知道我该如何解决这个问题?谢谢!
我的 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;
}
- (NSString *)getUserLatitude
{
NSString *userLatitude = [NSString stringWithFormat:@"%f",
locationManager.location.coordinate.latitude];
return userLatitude;
}
- (NSString *)getUserLongitude
{
NSString *userLongitude = [NSString stringWithFormat:@"%f",
locationManager.location.coordinate.longitude];
return userLongitude;
}
我正在使用这段代码从 appDelegate 获取更新的位置:
- (void)viewDidAppear:(BOOL)animated
{
NSString *userLatitude =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate
getUserLatitude];
NSString *userLongitude =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate
getUserLongitude];
}
最佳答案
位置管理器更新位置后,将调用可在此处使用的委托(delegate)。位置更新后,您将在 didUpdateToLocation 方法中获取该控件
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"Success ");
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"Failed %d",[error code]);
}
关于iphone - viewDidAppear 方法在 locationManager 可以定位用户之前调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14020991/