我在我的應用程序delegate
中調用locationManager
,並且回頭參考各種viewControllers
的viewDidAppear
方法中的getUserLatitude和getUserLongitude函數。viewDidAppear方法在locationManager之前調用可以找到用戶
我的問題是,我viewDidAppear
方法太快了appDelegate
定位用戶推出,而我得到0.000000的latitude
和0.000000爲longitude
。有誰知道我可以如何解決這個問題?謝謝!
我的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];
}
設置CLLocationManagerDelegate委託在你的委託和文件的.h文件按照我在我的答案中提到的。 – Girish