Ios8 如何在IOS SDK 8.0中获取当前位置纬度和经度
如何获取当前位置的纬度和经度 我试过这个。使用Xcode 6.01和IOS SDK 8.0Ios8 如何在IOS SDK 8.0中获取当前位置纬度和经度,ios8,core-location,cllocationmanager,cllocation,currentlocation,Ios8,Core Location,Cllocationmanager,Cllocation,Currentlocation,如何获取当前位置的纬度和经度 我试过这个。使用Xcode 6.01和IOS SDK 8.0 -(CLLocationCoordinate2D) getLocation{ locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; locationManager.desiredAccuracy = kCLLocationAccuracyBest; locat
-(CLLocationCoordinate2D) getLocation{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
return coordinate;
}
- (void)getCurrentLocation{
CLLocationCoordinate2D coordinate = [self getLocation];
NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];
NSLog(@"Latitude = %@", latitude);
NSLog(@"Longitude = %@", longitude);
}
- (void)viewDidLoad{
[super viewDidLoad];
[self getCurrentLocation];
}
在这里,我启用模拟器位置以部署8.0 iPhone simulator 4s。
即使在设备上,它也不工作
@全部
请告诉我,我做错了什么。是的,模拟器提供了过于简化的定位服务。物理设备是真正测试
CLLocationManager
行为的唯一方法
问题是,当您调用startUpdatingLocation
时,并不意味着您可以立即检索位置。(我想你会发现它要么是nil
,要么是水平精度为负值,这意味着位置尚未确定。)
您必须遵守CLLocationManagerDelegate
,并实现异步调用的locationManager:didUpdateLocations:
。因此,在尝试使用位置信息之前,必须等待调用此方法。而且,请注意,在实际设备上,您可能会看到此方法在设备细化设备位置信息时被多次调用,且精确度不断提高(例如,您可能会看到水平精度
数字从一个位置更新下降到另一个位置更新)
显然,您可能也想实现locationManager:didFailWithError:
,以便能够检测到问题。在调用startUpdatingLocation
之前,您可能还应该检查authorizationStatus
,locationServicesEnabled
,并在使用Authorization
时调用Requests。
我在IOS 8.0中解决了这个问题
NSLocationAlwaysUsageDescription将此手动添加到应用程序plist文件中,保留字符串值为空。
这将调用委托方法。永远不要忘记添加NSLocationAlwaysUsageDescription,除非您没有添加,否则此委托方法将不起作用。剩下的代码是相同的。将委托方法添加到控制器类
在locationManager声明代码中添加此行代码。适用于IOS 8.0
[locationManager请求始终授权] 在iOS 8中,此代码以静默方式失败,即您不会收到任何错误或警告
您需要做两件额外的事情才能使位置正常工作:
为您的信息添加密钥。plist
从位置管理器请求授权,请求启动
需要在Info.plist文件中添加以下任意一个或两个键
n不使用时的位置说明
n位置始终使用说明
这些是字符串类型,值可以是任何消息描述或空
现在您需要为相应的定位方法请求授权。使用以下任一呼叫:
[self.locationManager在使用授权时请求]
[self.locationManager请求始终授权]
添加了委托方法didFailWithError和didUpdateToLocation,但此方法根本不会触发。请在.h文件中添加CLLocationManagerDelegate,如@interface DemoViewController:UIViewControllerI添加了CLLocationManagerDelegate,但我仍然有问题。将sdk IOS 7升级到IOS 8后出现问题。