Search code examples
objective-cmkannotationviewcllocation

How to determine what coordinate is further North in objective-c


I'm writing a custom annotation view for my latest iOS 5 app and I'm looking for the latest SDK friendly way to compare 2 CLLocationCoordinate2D coordinates?

CLLocationCoordinate2D coordinate = (CLLocationCoordinate2D){33.0,-112.4};
CLLocationCoordinate2D coordinate = (CLLocationCoordinate2D){33.0,-112.3};

Is there a built in method to determine something like this?


Solution

  • You don't need a built in method. You only have to compare the latitude:

    CLLocationCoordinate2D coordinate1 = (CLLocationCoordinate2D){33.0,-112.4};
    CLLocationCoordinate2D coordinate2 = (CLLocationCoordinate2D){34.0,-112.3};
    
    CLLocationCoordinate2D furthestNorth;
    if (coordinate1.latitude > coordinate2.latitude) {
        furthestNorth = coordinate1;
    } else {
        furthestNorth = coordinate2;
    }
    // OR
    furthestNorth = (coordinate1.latitude > coordinate2.latitude) ? coordinate1 : coordinate2;