无法从我的代码中获取当前位置:
(IBAction)getdetails:(id)sender {
manager.delegate = self;
manager.desiredAccuracy = kCLLocationAccuracyBest;
[manager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate
(void)locationmanager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
_latitudelabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
_latitudelabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}
答案 0 :(得分:1)
在iOS 8中,您需要做两件额外的事情才能让位置正常工作:
在Info.plist中添加以下密钥:
NSLocationWhenInUseUsageDescription
和NSLocationAlwaysUsageDescription
请求位置管理员授权启动。
[locationManager requestWhenInUseAuthorization]; // or
[locationManager requestAlwaysAuthorization];
您可以从Apple Docs获取更多信息。
有关CoreLocation的重要信息,您还应该查看此Blog