代码:
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([[[NSUserDefaults standardUserDefaults] objectForKey:@"lat"] floatValue], [[[NSUserDefaults standardUserDefaults] objectForKey:@"lon"] floatValue]);
CLLocation *currentLocation = [[CLLocation alloc] initWithCoordinate:coordinate altitude:1 horizontalAccuracy:1 verticalAccuracy:-1 timestamp:nil];
[objects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([self.photos[idx][@"lat"] floatValue], [self.photos[idx][@"lon"] floatValue]);
CLLocation *location = [[CLLocation alloc] initWithCoordinate:coordinate altitude:1 horizontalAccuracy:1 verticalAccuracy:-1 timestamp:nil];
CLLocationDistance distance = [location distanceFromLocation:currentLocation]/1000;
}];
如果我NSLog当前位置和位置我得到相同的坐标,因为它们是相同的,有细微差别。位置为:lat:37.331581 lon:-122.030708,当前位置为:“lat”:“37.33154”,“lon”:“ - 122.0307”。如你所知,如果我从这两个地方得到距离,我应该得到0公里。但我得到的是12781.989811。这是为什么?我正在运行模拟器。如果是这种情况,或者我的代码错了?
答案 0 :(得分:1)
减少问题,使问题变得尽可能简单 有人试图回答的邮政编码可以运行 我们没有您的userDefauts,这使问题无法再现 添加显示问题的NSLog语句。
我怀疑如果你做这些事情,你将自己解决问题。
如何提出问题的示例:
CLLocationCoordinate2D coordinate1 = CLLocationCoordinate2DMake(37.331581, -122.030708);
CLLocation *location1 = [[CLLocation alloc] initWithCoordinate:coordinate1 altitude:1 horizontalAccuracy:1 verticalAccuracy:-1 timestamp:nil];
CLLocationCoordinate2D coordinate2 = CLLocationCoordinate2DMake(37.331581, -122.030708);
CLLocation *location2 = [[CLLocation alloc] initWithCoordinate:coordinate2 altitude:1 horizontalAccuracy:1 verticalAccuracy:-1 timestamp:nil];
CLLocationDistance distance1 = [location1 distanceFromLocation:location2]/1000;
NSLog(@"distance: %f", distance1);
CLLocationCoordinate2D coordinate3 = CLLocationCoordinate2DMake(37.33154, -122.0307);
CLLocation *location3 = [[CLLocation alloc] initWithCoordinate:coordinate3 altitude:1 horizontalAccuracy:1 verticalAccuracy:-1 timestamp:nil];
CLLocationDistance distance2 = [location1 distanceFromLocation:location3]/1000;
NSLog(@"distance: %f", distance2);
NSLog输出:
2013-11-06 09:28:17.661 Test-iOS[45090:70b] distance: 0.000000
2013-11-06 09:28:17.663 Test-iOS[45090:70b] distance: 0.004605
哦,没有错误!
最好的猜测是用户默认值不是您所期望的,或者枚举的输入不是您所期望的。