【发布时间】:2012-12-24 11:46:54
【问题描述】:
我在我的应用程序delegate 中调用了locationManager,并在各种viewControllers 的viewDidAppear 方法中引用了getUserLatitude 和getUserLongitude 函数。
我的问题是我的viewDidAppear 方法启动太快,appDelegate 无法定位用户,latitude 得到 0.000000,longitude 得到 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];
}
【问题讨论】:
-
在您的 .h 委托文件中设置 CLLocationManagerDelegate 委托,并按照我在回答中提到的操作。
标签: iphone objective-c ios cllocationmanager locationmanager