【问题标题】:CLLocationCoordinate2D latitude & longitude always zeroCLLocationCoordinate2D 纬度和经度始终为零
【发布时间】:2014-10-21 09:33:09
【问题描述】:

我正在尝试获取纬度和经度。我在实际设备上运行以下代码,但纬度和经度总是为零。我还导入库并设置委托。我可以知道什么是错的,怎么办?我的设备有 ios 8。

CLLocationManager *locationManager = [[CLLocationManager alloc] init];

if ([locationManager locationServicesEnabled])
{
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    [locationManager startUpdatingLocation];
}


CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];

NSString *str=[[NSString alloc] initWithFormat:@" latitude:%f longitude:%f",coordinate.latitude,coordinate.longitude];
NSLog(@"%@",str);

【问题讨论】:

  • 是否调用了委托方法?您需要按照此处的建议对 iOS 8 进行一些更改 stackoverflow.com/questions/25844430/…
  • 您是否将调试设置为某个位置?
  • 如何设置调试到位置? @noobsmcgoobs
  • 我的代表没有被叫到。我已将 NSLocationWhenInUseUsageDescription 和 NSLocationAlwaysUsageDescription 都放在我的 plist 中。当我调用位置时,我的应用会提示用户提醒是否允许。但是,我点击b4,它会自动关闭。 @Bhumit
  • 另外请注意,模拟器有问题,有时会给出随机的纬度和经度。我怀疑这与新的 OSX 有关。

标签: ios objective-c cllocationcoordinate2d


【解决方案1】:

您必须在委托方法中获取当前位置

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *currentLocation = [locations objectAtIndex:0];
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-11
相关资源
最近更新 更多