iphone - viewDidAppear 方法在 locationManager 可以定位用户之前调用

标签 iphone objective-c ios cllocationmanager locationmanager

我在我的应用程序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/

相关文章:

ios - 为什么在调用 [super prepareForReuse] 后删除了自定义 UITableViewCell 上 UIImageView 中的 'animations' 属性?

iphone - NSUrlConnection 委托(delegate) - 获取 http 状态代码

iphone - 最佳高分框架 - iPhone

ios - 从另一个 NSDictionary 中提取一个 NSDictionary

iphone - 在 UITableViewController 中设置标题栏文本的字体和字体大小

objective-c - 如何搜索 UITableView

ios - 如何将 CGRect 帧值传递给自定义过渡动画 Controller

ios - ios会在特定时间后终止在后台运行的应用程序吗?

iphone - 如何从另一个类文件调用函数

iphone - 防止应用程序崩溃