【问题标题】:CLLocationCoordinate2D returns integers for latitude and longitude instead of doubleCLLocationCoordinate2D 返回纬度和经度的整数,而不是双精度
【发布时间】:2014-02-04 06:33:12
【问题描述】:

我使用CLLocation获取当前位置如下:

-(void)loadCurrentLocation{
if (manager==nil) {
    manager=[[CLLocationManager alloc]init];
}

manager.delegate=self;
manager.desiredAccuracy=kCLLocationAccuracyBest;
[manager startUpdatingLocation];

}

- (void) viewDidLoad {
   [super viewDidLoad];
   [self loadCurrentLocation];

 }
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
CLLocation *loc=newLocation;
if (loc!=nil) {
    self.latitude=loc.coordinate.latitude; //1
    self.longitude=loc.coordinate.longitude; //2
    NSLog(@"Gained Latitude:%.f",self.latitude);
    NSLog(@"Gained Longitude:%.f",self.longitude);
}

}

鉴于latitudelongitude.h 文件中声明如下:

@interface Prayers :UIViewController<CLLocationManagerDelegate>
@property double longitude;
@property double latitude;
@end

问题在于第 1 行和第 2 行的返回值是整数,如 30 和 31,我期待它们像 31.37703360000000400030.016893900000000000,那么为什么返回值是整数而不是 double 呢?在此先感谢

【问题讨论】:

    标签: objective-c cocoa-touch ios7 cllocationcoordinate2d


    【解决方案1】:

    如果类型不是您所期望的,Xcode 可能会给您转换警告。是什么让你认为他们不是你要回来的双打?也许框架只是四舍五入到最接近的度数并将其作为双精度数返回?

    另一种可能性。如果你的日志看起来像什么

    NSLog(@"Gained Latitude:%.2f",self.latitude);
    

    这样打印是否更准确(注意,格式中的 2)。


    甚至可以尝试将它们装箱为 NSNumber 并查看打印的内容:

        NSLog(@"Gained Latitude:%@", @(self.latitude));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-01
      • 2015-03-20
      • 2015-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多