我想获得当前位置但不能获取

时间:2015-05-19 11:56:15

标签: ios

无法从我的代码中获取当前位置:

(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];
    }

1 个答案:

答案 0 :(得分:1)

在iOS 8中,您需要做两件额外的事情才能让位置正常工作:

  1. 在Info.plist中添加以下密钥: NSLocationWhenInUseUsageDescriptionNSLocationAlwaysUsageDescription

  2. 请求位置管理员授权启动。

    [locationManager requestWhenInUseAuthorization];    // or 
    [locationManager requestAlwaysAuthorization];
    
  3. 您可以从Apple Docs获取更多信息。

    有关CoreLocation的重要信息,您还应该查看此Blog

相关问题