【问题标题】:Unable to get longitude and latitude in ios8 by GPSios8无法通过GPS获取经度和纬度
【发布时间】:2015-07-31 08:53:52
【问题描述】:

我正在使用 iOS8 并使用以下代码通过 GPS 获取经度和纬度:

-(void)CurrentLocationIdentifier
{
    locationManager = [CLLocationManager new];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 &&
    [CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse)
    //[CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedAlways)
    {
    // Will open an confirm dialog to get user's approval
    [locationManager requestWhenInUseAuthorization];
    //[_locationManager requestAlwaysAuthorization];
    } else {
    [locationManager startUpdatingLocation]; //Will update location immediately
    }
}

- (void)locationManager:(CLLocationManager *)manager
 didUpdateLocations:(NSArray *)locations {
    CLLocation *location = [locations lastObject];
    NSLog(@"lat%f - lon%f", location.coordinate.latitude,   location.coordinate.longitude);
 }

- (void)locationManager:(CLLocationManager*)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    switch (status) {
    case kCLAuthorizationStatusNotDetermined: {
        NSLog(@"User still thinking..");
    }
    case kCLAuthorizationStatusDenied: {
        NSLog(@"User hates you");
    } break;
    case kCLAuthorizationStatusAuthorizedWhenInUse:
    case kCLAuthorizationStatusAuthorizedAlways: {
        [locationManager startUpdatingLocation]; //Will update location immediately
    } break;
    default:
        break;
}
}

运行此代码后,我在日志中得到的输出是:用户仍在思考。谁能帮我解决这个问题,我是 iOS 和 CoreLocation.Framework 的新手? 请帮助我找出错误以及如何解决。

【问题讨论】:

标签: ios ios8 gps location core-location


【解决方案1】:

在 InfoPlist.strings 文件中添加此字符串

1) NSLocationWhenInUseUsageDescription

2) NSLocationAlwaysUsageDescription

试试这个代码

locationManagerApp=[[CLLocationManager alloc] init];

    locationManagerApp.delegate = self;
    locationManagerApp.distanceFilter = kCLDistanceFilterNone;
    locationManagerApp.desiredAccuracy = kCLLocationAccuracyHundredMeters;

    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    {        
           [locationManagerApp requestAlwaysAuthorization];
    }

    [locationManagerApp startUpdatingLocation];
    CLLocation *location1 = [locationManagerApp location];
    CLLocationCoordinate2D coordinate = [location1 coordinate];
    self.latValue= [NSString stringWithFormat:@"%f", coordinate.latitude];
    self.longValue = [NSString stringWithFormat:@"%f", coordinate.longitude];
    NSLog(@"Latitude  = %@", self.latValue);
    NSLog(@"Longitude = %@", self.longValue);

并在设备中运行项目。

【讨论】:

  • 首先很抱歉回复晚了,但我想在模拟器中运行它,我已经完成了调试>模拟位置>选项和模拟器调试>位置>Apple等设置。您能就此提出任何建议吗?
  • 感谢您的帮助和建议。我在 infoPlist.strings 文件中添加了字符串,它起作用了。
【解决方案2】:

在 iOS 8 中,您需要做两件额外的事情才能让定位工作:向您的 Info.plist 添加一个密钥,并从定位管理器请求授权以启动它。新的位置授权有两个 Info.plist 键。需要这些密钥中的一个或两个。如果两个键都不存在,您可以调用 startUpdatingLocation 但位置管理器实际上不会启动。它也不会向代理发送失败消息(因为它从未启动,所以它不会失败)。如果您添加一个或两个密钥但忘记明确请求授权,它也会失败。

因此,您需要做的第一件事是将以下一个或两个键添加到您的 Info.plist 文件中:

NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription

这两个键都带有一个字符串,该字符串描述了您需要位置服务的原因。您可以输入一个字符串,如“需要位置才能找出您所在的位置”,与 iOS 7 一样,可以在 InfoPlist.strings 文件中进行本地化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多