【问题标题】:I want to get current location but cant fetch [closed]我想获取当前位置但无法获取 [关闭]
【发布时间】:2015-07-31 05:48:34
【问题描述】:

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

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

【问题讨论】:

  • 请解决这个问题..我还添加了 tw 框架工作核心位置,mapkit..
  • 请正确格式化您的问题。使用- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locationsReference
  • 请用您面临的问题更新您的问题,并查看其格式是否正确,以便希望提供帮助的人可以这样做。

标签: ios


【解决方案1】:

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

  1. 将以下键添加到您的 Info.plist: NSLocationWhenInUseUsageDescriptionNSLocationAlwaysUsageDescription

  2. 向位置管理器请求授权,要求它开始。

    [locationManager requestWhenInUseAuthorization];    // or 
    [locationManager requestAlwaysAuthorization];
    

您可以从Apple Docs获取更多信息。

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

【讨论】:

  • 这是一个正确的解决方案。
猜你喜欢
  • 1970-01-01
  • 2012-10-30
  • 1970-01-01
  • 2014-05-31
  • 1970-01-01
  • 2022-10-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多