2013-01-06 69 views
0

我的应用程序获取用户在appDelegate中的位置并在某些viewControllers的viewDidAppear方法中调用该位置。我的问题是,第一次viewController的负载,没有足够的时间来获取用户的位置。从viewDidAppear方法调用viewController的方法

这里是我的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; 
} 

这里是我使用的是什么,从viewControllers调用的位置:

- (void) viewDidAppear:(BOOL)animated 
{ 
NSString *userLatitude =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate 
getUserLatitude]; 

NSString *userLongitude =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate 
getUserLongitude]; 
} 

人对如何解决任何想法?非常感谢!

回答

0

不知道为什么你张贴同样的问题两次......但看到我的回答你的其他职位,

Pass Coordinates from locationManager in appDelegate to viewController

总之,你需要实现CoreLocation的委托方法,因为这些方法在找到新位置时调用。在这些委托方法中,引发NSNotification(ViewControllers)订阅以获取新用户的位置。

1

尝试在这里使用全局变量。它会随着您的位置更新而更新,并且您将始终更新位置坐标。

-(void)didUpdateLocation // location update method of CLLocation Manager class 
{ 
// assign current ordinates values to global variables. 
} 
相关问题