How can I compare CLLocationCoordinate2D

The Crazy Chimp picture The Crazy Chimp · Apr 17, 2012 · Viewed 23.9k times · Source

I need a way of comparing two CLLocationCoordinate2D's however when I tried using == it wouldn't work. Please can someone help me out with the best way of comparing them?

Answer

Glenn picture Glenn · Apr 17, 2012

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.