我正在使用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);NSLogged的距离是12769908。我认为这是不正确的,因此我的代码一定有问题。
如果有,请您指点一下!
发布于 2011-03-05 08:22:19
你有两个坏习惯。
-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!发布于 2011-03-05 04:42:34
尝试@"%f“,不要以这种方式转换它。
在CLLocation.h中
typedef double CLLocationDistance;https://stackoverflow.com/questions/5198996
复制相似问题