-3

编码:

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 currentLocation 和我得到相同坐标的位置,因为它们相同但有细微差别。位置是:lat:37.331581 lon:-122.030708,当前位置是:“lat”:“37.33154”,“lon”:“-122.0307”。正如你所看到的,如果我得到这两个位置的距离,我应该得到 0 公里。但我得到了 12781.989811。这是为什么?我正在运行模拟器。如果是这样,还是我的代码错误?

4

1 回答 1

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

哦,没有错误!

最好的猜测是用户默认值不是您所期望的,或者枚举的输入不是您所期望的。

于 2013-11-06T14:31:43.377 回答