【发布时间】:2011-06-15 09:11:27
【问题描述】:
我已经定义了一个名为 FGLocation 的类,它符合 MKAnnotation 协议,我想做的是测量其中两个对象之间的距离。我注意到我可以使用方法 distanceFromLocation: 定义为 CLLocation 类的方法。正如您在下面看到的,我正在创建两个临时 CLLocation 对象来进行计算,但我不禁想到我可能错过了更好/更简单的方法。有没有人对我正在做什么或如何做得更好有任何意见?
// IS THERE A BETTER WAY TO DO THIS?
FGLocation *root = [annotations objectAtIndex:counter-1];
FGLocation *leaf = [annotations objectAtIndex:counter];
CLLocation *rootLocation = [[CLLocation alloc] initWithLatitude:[root coordinate].latitude longitude:[root coordinate].longitude];
CLLocation *leafLocation = [[CLLocation alloc] initWithLatitude:[leaf coordinate].latitude longitude:[leaf coordinate].longitude];
CLLocationDistance thisDistance = [rootLocation distanceFromLocation:leafLocation];
[rootLocation release];
[leafLocation release];
注意:我的 FGLocation 对象定义为(见下文)我在文档中注意到我不应该继承 CLLocation。
@interface FGLocation : NSObject <MKAnnotation> {
【问题讨论】:
标签: iphone objective-c cocoa-touch