我如何比较CLLocationCoordinate2D

问题描述 投票:47回答:10

我需要一种比较两个CLLocationCoordinate2D的方法然而当我尝试使用==时它不起作用。请有人帮助我比较它们的最佳方法吗?

core-location cllocation equatable
10个回答
56
投票

用于比较任何给定结构类型的两个实例的通用方法:

memcmp(&cllc2d1, &second_cllc2d, sizeof(CLLocationCoordinate2D))

要么

cllc2d1.latitude == cllc2d2.latitude && cllc2d1.longitude == cllc2d2.longitude

应该工作,如果你真的想确定他们是完全平等的。但是,鉴于纬度和经度被定义为双精度,您可能真的想要进行“足够接近”的比较:

fabs(cllc2d1.latitude - cllc2d2.latitude) <= epsilon && fabs(cllc2d1.longitude - cllc2d2.longitude) <= epsilon

其中epsilon是您想要接受的任何错误级别。


0
投票

我们可以将纬度和经度转换为NSString并进行字符串比较。

+ (BOOL)isEqualWithCoordinate:(CLLocationCoordinate2D)location1 withAnotherCoordinate:(CLLocationCoordinate2D)location2{
NSString *locationString1 = [NSString stringWithFormat:@"%g, %g", location1.latitude, location1.longitude];
NSString *locationString2 = [NSString stringWithFormat:@"%g, %g", location2.latitude, location2.longitude];

if ([locationString1 isEqualToString:locationString2]) {
    return YES;
}else{
    return NO;
}

}

由于%g会将截断十进制数转换为4位数。


33
投票

作为所有这些答案的一个小补充,将比较定义为预处理器定义是非常方便的:

#define CLCOORDINATES_EQUAL( coord1, coord2 ) (coord1.latitude == coord2.latitude && coord1.longitude == coord2.longitude)

或者与epsilon:

#define CLCOORDINATE_EPSILON 0.005f
#define CLCOORDINATES_EQUAL2( coord1, coord2 ) (fabs(coord1.latitude - coord2.latitude) < CLCOORDINATE_EPSILON && fabs(coord1.longitude - coord2.longitude) < CLCOORDINATE_EPSILON)

这允许您进行如下比较:

CLLocationCoordinate2D resultingCoordinate = ... a method call ...;
CLLocationCoordinate2D expectedCoordinate = CLLocationCoordinate2DMake(48.11, 11.12);

if(CLCOORDINATES_EQUAL( resultingCoordinate, expectedCoordinate)) {
    NSLog(@"equal");
} else {
    NSLog(@"not equal");
}

如果您不喜欢预处理器,另一种方法是使用内联方法。


19
投票

Swift扩展:

import MapKit

extension CLLocationCoordinate2D: Equatable {}

public func ==(lhs: CLLocationCoordinate2D, rhs: CLLocationCoordinate2D) -> Bool {
    return (lhs.latitude == rhs.latitude && lhs.longitude == rhs.longitude)
}

[从Xcode 7.3.1开始测试,Swift 2.2] [当然,仍然存在比较浮点值的内在危险,因此您可能需要考虑使用前面答案中提到的epsilon]


12
投票

您可以使用CLLocation类的distanceFromLocation:方法。返回值是CLLocationDistance,实际上只是一个双倍。

- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location

8
投票

您可以定义一个感觉非常类似于来自CoreLocation的函数:

BOOL CLLocationCoordinateEqual(CLLocationCoordinate2D coordinate1, CLLocationCoordinate2D coordinate2) { return (fabs(coordinate1.latitude - coordinate2.latitude) <= DBL_EPSILON && fabs(coordinate1.longitude - coordinate2.longitude) <= DBL_EPSILON); }


6
投票

在Swift 3中,DBL_EPSILON已被弃用。使用Double.ulpOfOne

extension CLLocationCoordinate2D {
    func isEqual(_ coord: CLLocationCoordinate2D) -> Bool {
        return (fabs(self.latitude - coord.latitude) < .ulpOfOne) && (fabs(self.longitude - coord.longitude) < .ulpOfOne)
    }
}

2
投票
CLLocationCoordinate2D c1, c2;
if (c1.latitude == c2.latitude && c1.longitude == c2.longitude)
{
    // ...
}

我不是在开玩笑。 CLLocationCoordinate2D是一个C结构,除了比较各个成员之外,没有简单的方法来比较C结构。


1
投票

CLLocationCoordinate2D是一个C结构,因此,您需要比较它的字段:

CLLocationCoordinate2D coord1, coord2;
if (coord1.latitude == coord2.latitude && coord1.longitude == coord2.longitude) {
    // coordinates are equal
}

注意,CLLocationCoordinate2D字段是double,因此,您可能会遇到与任何其他浮点比较相同的问题。因此,我建议舍入一点点数值,例如:

CLLocationCoordinate2D coord1, coord2;
if (round(coord1.latitude * 1000.0) == round(coord2.latitude * 1000.0)
    && round(coord1.longitude * 1000.0) == round(coord2.longitude * 1000.0)) {
    // coordinates are equal
}

是的,这样,代码速度较慢,但​​这可以帮助您避免由浮点数不太完美的精度引起的问题。


1
投票

你可以使用这个函数CLLocationCoordinateNSValue包装成+ (NSValue *)valueWithMKCoordinate:(CLLocationCoordinate2D)coordinate

然后使用isEqualToValue进行比较。

来自Apple doc of isEqualToValue函数的引用:

NSValue类比较每个值对象的类型和内容以确定相等性。

参考:NSValue的Apple doc

© www.soinside.com 2019 - 2024. All rights reserved.