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

Objective C / iPhone比较2个CLLocations / GPS坐标

发布时间:2020-12-14 18:57:45 所属栏目:百科 来源:网络整理
导读:有一个应用程序可以成功找到您的GPS位置,但我需要能够将该GPS与GPS位置列表进行比较,如果两者相同,那么您将获得奖励. 我以为我有它工作,但似乎没有. 我有’newLocation’作为你所在的位置,我认为问题是我需要能够分离newLocation的long和lat数据. 到目前为止
有一个应用程序可以成功找到您的GPS位置,但我需要能够将该GPS与GPS位置列表进行比较,如果两者相同,那么您将获得奖励.

我以为我有它工作,但似乎没有.

我有’newLocation’作为你所在的位置,我认为问题是我需要能够分离newLocation的long和lat数据.

到目前为止,我试过这个:

NSString *latitudeVar = [[NSString alloc] initWithFormat:@"%g°",newLocation.coordinate.latitude];

NSString *longitudeVar = [[NSString alloc] initWithFormat:@"%g°",newLocation.coordinate.longitude];

GPS位置列表的示例:

location:(CLLocation*)newLocation;

CLLocationCoordinate2D bonusOne;    

bonusOne.latitude = 37.331689;
bonusOne.longitude = -122.030731;

然后

if (latitudeVar == bonusOne.latitude && longitudeVar == bonusOne.longitude) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"infinite loop firday" message:@"infloop" delegate:nil cancelButtonTitle:@"Stinky" otherButtonTitles:nil ];    

    [alert show];
    [alert release];
}

这会产生一个错误’无效操作数到二进制==有strut NSstring和CLlocationDegrees’

有什么想法吗?

解决方法

通常,您应该小心直接比较浮点数.由于它们的定义方式,内部值可能与初始化它们不完全相同,这意味着它们很少相同.相反,您应该检查它们之间的差异是否低于某个阈值

if(fabs(latitude1 - latitude2) <= 0.000001)
...

另一种选择可以是通过计算距离来检查人与所需位置的距离.这也可以考虑到GPS的坐标不完全正确的事实,但即使在良好的条件下也可能相差10米:

CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:lat1 longitude:lon1];
double distance = [loc1 getDistanceFrom:position2];
if(distance <= 10)
...

克劳斯

(编辑:李大同)

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

    推荐文章
      热点阅读