How can I compare CLLocationCoordinate2D

Either the generic approach for comparing two instances of any given struct type:

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

or

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

should work, if you really want to be sure they're exactly equal. However, given that latitude and longitude are defined as doubles, you might really want to do a "close enough" comparison:

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

where epsilon is whatever level of error you want to accept.


As a small addition to all these answers, it's quite handy to have the comparison defined as a preprocessor define:

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

or with 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)

This allows you to do a comparison as follows:

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

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

Another alternative is to use an inline method, if you don't like the preprocessor.


A Swift extension:

import MapKit

extension CLLocationCoordinate2D: Equatable {}

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

[tested as of Xcode 7.3.1, Swift 2.2] [and, of course, still has the intrinsic danger of comparing floating point values, so you might want to consider using epsilon as mentioned in earlier answers]


You can use the CLLocation class' distanceFromLocation: method. The return value is a CLLocationDistance, which is really just a double.

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