iphone – CLLocation distanceFromLocation
我正在使用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.我认为这是不正确的,因此我的代码一定有问题. 如果有,请你指出来! 解决方法
你有两个坏习惯.
>在需要硬件审查状态的情况下,您不应该依赖模拟器.特别是当你想要正确的测试. 您的经度值超出范围.这意味着其中之一.您错误地打印了值或实际值是错误的.模拟器可以打印错误的值.或者您使用错误的方法打印了值.你得试试:
如果在那之后继续糟糕的结果,
而且,我建议像这样检查所有中间值. CLLocationCoordinate2D annocoord = annotation.coordinate; CLLocationCoordinate2D usercoord = self.mapView.userLocation.coordinate; NSLog(@"ANNO = %f,%f",annocoord.latitude,annocoord.longitude); NSLog(@"USER = %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,loc.coordinate.latitude,loc.coordinate.longitude); NSLog(@"LOC2 = %f,loc2.coordinate.latitude,loc2.coordinate.longitude); CLLocationDistance dist = [loc distanceFromLocation:loc2]; NSLog(@"DIST: %f",dist); // Wrong formatting may show wrong value! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |