加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – CLLocation distanceFromLocation

发布时间:2020-12-14 18:08:18 所属栏目:百科 来源:网络整理
导读:我正在使用CLLocation来计算当前用户位置和注释的距离.但是我只是想知道这是否正确.我目前正在使用iPhone模拟器,根据MKMapView,iPhone模拟器位于: Lat: 0 Long: -1067024384 注释的位置是: workingCoordinate.latitude = 40.763856;workingCoordinate.long
我正在使用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.我认为这是不正确的,因此我的代码一定有问题.

如果有,请你指出来!

解决方法

你有两个坏习惯.

>在需要硬件审查状态的情况下,您不应该依赖模拟器.特别是当你想要正确的测试.
>您正在错误地处理类型.因此您无法正确检查值.经度-1067024384怎么样?经度值是度.这意味着根据经度的定义,它的有效范围限制在-90.0~90.0.

您的经度值超出范围.这意味着其中之一.您错误地打印了值或实际值是错误的.模拟器可以打印错误的值.或者您使用错误的方法打印了值.你得试试:

Test on real device which has real hardware censors.

如果在那之后继续糟糕的结果,

Review ALL of your application code.
Especially for printing,handling values. Check you’re using correct types and castings in > each situations. Because you may did buggy operation in somewhere habitually.

而且,我建议像这样检查所有中间值.

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!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读