-1

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);
}

}

鉴于此,latitude并且longitude在文件中声明如下.h

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

问题是第 1 行和第 2 行的返回值是像 30 和 31 这样的整数,我期待它们像31.377033600000004000and一样30.016893900000000000,那么为什么返回的值是整数而不是 double 呢?提前致谢

4

1 回答 1

1

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

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

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

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


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

    NSLog(@"Gained Latitude:%@", @(self.latitude));
于 2014-02-04T06:39:13.977 回答