我正在尝试获取纬度和经度。我在实际设备上运行以下代码,但纬度和经度总是为零。我还导入库并设置委托(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/