CLLocation distanceFromLocation

4

我正在使用CLLocation来计算当前用户位置与标注之间的距离。不过我只是想知道这是否正确。目前我正在使用iPhone模拟器进行测试,根据MKMapView显示,iPhone模拟器位于这里:

Lat: 0 Long: -1067024384

注释的位置是:
workingCoordinate.latitude = 40.763856;
workingCoordinate.longitude = -73.973034;

然而,如果你在谷歌地图上查看,你会发现这些距离是多么接近,但根据CLLocation来说是远离的。我正在使用以下代码来确定它们之间的距离。

CLLocation *loc = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:self.mapView.userLocation.coordinate.latitude longitude:self.mapView.userLocation.coordinate.longitude];
CLLocationDistance dist = [loc distanceFromLocation:loc2];
int distance = dist
NSLog(@"%i", distance);

被NSLog记录的距离为12769908。我认为这是不正确的,因此我的代码肯定出了问题。

如果有问题,请指出来!

2个回答

9
你有两个不好的习惯。
  1. 在需要硬件传感器状态的情况下,你不应该依赖模拟器。特别是当你想进行正确的测试时。
  2. 你处理类型不正确。因此,你无法正确检查值。经度为 -1067024384 是什么意思?经度值是以度为单位的。这意味着它的有效范围根据经度的定义是 -90.0 ~ +90.0。

您的经度值超出了范围。这意味着以下情况之一。您错误地打印了该值或实际值错误。模拟器可能会打印错误的值。或者您使用了错误的方法打印该值。您必须尝试:

在具有真实硬件传感器的真实设备上进行测试。

如果此后结果仍然不佳,请

检查您的所有应用程序代码。 特别是打印、处理值。检查您在每种情况下是否使用了正确的类型和转换。因为您可能在某个地方习惯性地执行了错误操作。

此外,我建议检查所有中间值,如下所示。

CLLocationCoordinate2D annocoord = annotation.coordinate;
CLLocationCoordinate2D usercoord = self.mapView.userLocation.coordinate;

NSLog(@"ANNO  = %f, %f", annocoord.latitude, annocoord.longitude);
NSLog(@"USER = %f, %f", usercoord.latitude, usercoord.longitude);

CLLocation *loc = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:self.mapView.userLocation.coordinate.latitude longitude:self.mapView.userLocation.coordinate.longitude];

NSLog(@"LOC  = %f, %f", loc.coordinate.latitude,  loc.coordinate.longitude);
NSLog(@"LOC2 = %f, %f", loc2.coordinate.latitude, loc2.coordinate.longitude);

CLLocationDistance dist = [loc distanceFromLocation:loc2];

NSLog(@"DIST: %f", dist); // Wrong formatting may show wrong value!

我在设备上遇到了相同的错误。这是您提供的代码的输出: ANNO = 37.331741, -122.030564 USER = -180.000000, -180.000000 LOC = 37.331741, LOC2 = -180.000000, -180.000000 LOC2 = -180.000000, -180.000000 DIST: 12769908.162754 - max_
我仍然像以前一样得到随机位置。 - max_

1
尝试使用@"%f",不要那样转换它。
CLLocation.h中。
typedef double CLLocationDistance;

那并没有产生任何影响。我已经将int改为float,在控制台中返回相同的结果。我还尝试将其更改为double。 - max_
12769908 = 12769公里和908米... 顺便说一下,你的模拟器显示的位置非常奇怪。 - bioffe
我可以问一下你是怎么算出来的吗?它怎么可能等于12769公里?但是908米听起来更为真实。 - max_
12769908米 = 12769公里加上908米。'distanceFromLocation:'函数返回距离(单位为米)。 - bioffe
@XcodeDev 当你在printf数据类型不匹配时,输出可能会完全偏离。一旦将其转换为双精度,你能告诉它显示什么输出吗?顺便说一下,我的模拟器默认位置看起来像这样 newLocation=<+40.71815700, -74.03419350> +/- 150.00m (speed 0.00 mps / course -1.00) - bioffe
嘿,我找到了注释距离错误的原因。但是我在我的itouch上运行应用程序时,NSLogged距离不正确,它显示为-180.000000和-180.000000:CLLocationCoordinate2D usrloc = self.mapView.userLocation.coordinate; NSLog(@“Long:%f Lat:%f USRLOC”,usrloc.longitude,usrloc.latitude); NSLog(@“Long:%f Lat:%f ANNOTATION”,appleStore2.coordinate.longitude,appleStore2.coordinate.latitude); //正确 - max_

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接