ios - CLLocationCoordinate2D 纬度和经度始终为零

标签 ios objective-c cllocationcoordinate2d

我正在尝试获取纬度和经度。我在实际设备上运行以下代码,但纬度和经度总是为零。我还导入库并设置委托(delegate)。我可以知道哪里出了问题以及该怎么办吗?我的设备有 ios 8。

CLLocationManager *locationManager = [[CLLocationManager alloc] init];

if ([locationManager locationServicesEnabled])
{
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    [locationManager startUpdatingLocation];
}


CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];

NSString *str=[[NSString alloc] initWithFormat:@" latitude:%f longitude:%f",coordinate.latitude,coordinate.longitude];
NSLog(@"%@",str);

最佳答案

你必须在委托(delegate)方法中获取当前位置

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *currentLocation = [locations objectAtIndex:0];
}

关于ios - CLLocationCoordinate2D 纬度和经度始终为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26483406/

相关文章:

ios - 如何仅在 Swift iOS 的最后一个屏幕上获取导航栏?

ios - 在 xcode 项目中的目标之间共享数据

ios - 自定义 UITableViewCell,重新排序时删除初始 View

ios - 无论 map 提供商如何,一个 CLlocation 2D 是否唯一对应于地球上的一个地方?

swift - CLLocationCoordinate2D 到一个数组

ios - 如何将 NSValue 转换为 CLLocationCoordinate2D

ios - 什么是最好的解决方案?扫描

ios - 如何一次将更改应用于多个 View Controller ?

android - 我们是否有任何机制来跟踪应用程序是否是从给定 URL 安装的?

ios - 为什么在 iOS 9 和 10 上无法调用 AVPlayer 的 'status' 属性上的 KVO