如何在objective-c中确定哪个坐标更靠北

时间:2012-05-17 00:58:36

标签: objective-c mkannotationview cllocation

我正在为我最新的iOS 5应用编写自定义注释视图,我正在寻找最新的SDK友好方式来比较2个CLLocationCoordinate2D坐标?

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

是否有内置的方法来确定这样的东西?

1 个答案:

答案 0 :(得分:2)

您不需要内置方法。您只需要比较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;