Since you stated that the two different points are “contacts,” I assume that you are using MKPinAnnotationView (or some other kind of annotation view). If not, you will have to find a place in another way.
If you have pointers to annotation objects for these locations, you can easily call -coordinate
onto them, create CLLocations from these coordinates (using -initWithLatitude:longitude:
), and then use the method -getDistanceFrom
to extract the distance in meters. From there it easily turns into miles. Everything says that the code will look something like this:
CLLocationCoordinate2D pointACoordinate = [pointAAnnotation coordinate];
CLLocation *pointALocation = [[CLLocation alloc] initWithLatitude:pointACoordinate.latitude longitude:pointACoordinate.longitude];
CLLocationCoordinate2D pointBCoordinate = [pointBAnnotation coordinate];
CLLocation *pointBLocation = [[CLLocation alloc] initWithLatitude:pointBCoordinate.latitude longitude:pointBCoordinate.longitude];
double distanceMeters = [pointALocation getDistanceFrom:pointBLocation];
double distanceMiles = (distanceMeters / 1609.344);
As a result, you get the distance in miles and can compare it from there. Hope this helps.
source
share