如何测试两个 CLLocation 的相等性

发布于 2024-11-17 14:22:07 字数 278 浏览 3 评论 0原文

我遇到了 isEqual 问题:

代码:

 if (currentAnchor isEqual:currentBusiness.getCllLocation))
    {
        do a;
    }
    else
    {
        do b;
    }

currentanchor 和 currentbusiness.getCllocation 是位置

但是如果它们相同,为什么调用函数 b?我的代码有问题吗?

I'm having a problem with isEqual:

The code:

 if (currentAnchor isEqual:currentBusiness.getCllLocation))
    {
        do a;
    }
    else
    {
        do b;
    }

currentanchor and currentbusiness.getCllocation are locations

But if they are the same, why is function b called? Is something wrong with my code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁月染过的梦 2024-11-24 14:22:07

根据 getClLocation 的名称,我假设这两个对象都是 CLLocation 类型。

CLLocation 没有任何关于其 isEqual: 方法的规范,因此它可能只是继承 NSObject 的实现,它只是比较对象的指针。如果您有两个具有相同数据的不同对象,则 isEqual: 实现将返回 NO。如果你有两个不同的物体,只是位置略有不同,那么它们肯定是不相等的。

比较位置对象时,您可能不需要 isEqual:。相反,您可能希望在 CLLocation 上使用 distanceFromLocation: 方法。像这样的东西会更好:

CLLocationDistance distanceThreshold = 2.0; // in meters
if ([currentAnchor distanceFromLocation:currentBusiness.getCllLocation] < distanceThreshold)
{
  do a;
}
else
{
  do b;
}

I assume both of these objects are of type CLLocation, based on the name of getClLocation.

CLLocation doesn't have any specification on what its isEqual: method does, so it's likely just inheriting the implementation of NSObject, which simply compares the pointers of the objects. If you've got two distinct objects with identical data, that isEqual: implementation would return NO. And if you've got two distinct objects with just a slight variation in location, they definitely would not be equal.

You probably don't want isEqual: when comparing location objects. Rather, you probably want to use the distanceFromLocation: method on CLLocation. Something like this would be better:

CLLocationDistance distanceThreshold = 2.0; // in meters
if ([currentAnchor distanceFromLocation:currentBusiness.getCllLocation] < distanceThreshold)
{
  do a;
}
else
{
  do b;
}
雨后彩虹 2024-11-24 14:22:07

已经有一段时间了。

我对 BJ Homer 所做的与此类似。我只是添加这个。

@interface CLLocation  (equal)
- (BOOL)isEqual:(CLLocation *)other;
@end

@implementation CLLocation  (equal)

- (BOOL)isEqual:(CLLocation *)other {


    if ([self distanceFromLocation:other] ==0)
    {
        return true;
    }
    return false;
}
@end

我很惊讶我是问这个问题的人:)

It's been a while.

What I did is similar with BJ Homer. I just add this.

@interface CLLocation  (equal)
- (BOOL)isEqual:(CLLocation *)other;
@end

@implementation CLLocation  (equal)

- (BOOL)isEqual:(CLLocation *)other {


    if ([self distanceFromLocation:other] ==0)
    {
        return true;
    }
    return false;
}
@end

I was surprised I were the one asking this question :)

放我走吧 2024-11-24 14:22:07

isEqual 仅检查对象而不检查其内容。您需要创建自己的方法,在其中访问对象的变量并使用 == 运算符检查它们是否相等。

isEqual just check only objects not their content. you require to create your own method where you access the variables of object and check them for equality using == operator.

红衣飘飘貌似仙 2024-11-24 14:22:07

斯威夫特4.0版本:

let distanceThreshold = 2.0 // meters
if location.distance(from: CLLocation.init(latitude: annotation.coordinate.latitude,
                                           longitude: annotation.coordinate.longitude)) < distanceThreshold
{ 
    // do a
} else {
    // do b
}

Swift 4.0 version:

let distanceThreshold = 2.0 // meters
if location.distance(from: CLLocation.init(latitude: annotation.coordinate.latitude,
                                           longitude: annotation.coordinate.longitude)) < distanceThreshold
{ 
    // do a
} else {
    // do b
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文