Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为什么同一坐标使用distanceFromLocation获得更大的距离_Ios_Iphone_Cllocation - Fatal编程技术网

Ios 为什么同一坐标使用distanceFromLocation获得更大的距离

Ios 为什么同一坐标使用distanceFromLocation获得更大的距离,ios,iphone,cllocation,Ios,Iphone,Cllocation,守则: CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([[[NSUserDefaults standardUserDefaults] objectForKey:@"lat"] floatValue], [[[NSUserDefaults standardUserDefaults] objectForKey:@"lon"] floatValue]); CLLocation *currentLocation = [[CL

守则:

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

如果我记录currentLocation和location,我会得到相同的坐标,因为它们是相同的,只有细微的差别。位置为:lat:37.331581 lon:-122.030708,当前位置为:“lat”:“37.33154”,“lon”:“-122.0307”。你可以看到,如果我得到这两个位置的距离,我应该得到0公里。但是我得到了12781.9811。为什么呢?我正在运行模拟器。如果是这种情况,或者我的代码是错误的?

减少问题,即尽可能简单地解决同样的问题。
试图回答的人可以运行的Post代码。
我们没有您的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
哦,没有错


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

Nah,该方法返回给定坐标的
0.004605
。如何记录距离和坐标?distanceFromLocation方法本身的行为是相同的,无论是在模拟器中运行还是在设备中运行。结果仅取决于位置的坐标。不相关,但您应该使用doubleValue而不是floatValue以获得更高的准确性(而且因为CLLocationDegrees是双精度的)。是否可能因为我使用enumerate方法,所以没有得到我期望的结果?对此表示抱歉。我会尽量使它更清楚和合理。