【发布时间】:2014-02-26 10:43:58
【问题描述】:
我正在尝试为 MKPolygon 创建一个面积计算类别。 我找到了一些 JS 代码 https://github.com/mapbox/geojson-area/blob/master/index.js#L1 与算法的链接:http://trs-new.jpl.nasa.gov/dspace/handle/2014/40409。 它说:
这是我的代码,它给出了错误的结果(比实际多出数千倍):
#define kEarthRadius 6378137
@implementation MKPolygon (AreaCalculation)
- (double) area {
double area = 0;
NSArray *coords = [self coordinates];
if (coords.count > 2) {
CLLocationCoordinate2D p1, p2;
for (int i = 0; i < coords.count - 1; i++) {
p1 = [coords[i] MKCoordinateValue];
p2 = [coords[i + 1] MKCoordinateValue];
area += degreesToRadians(p2.longitude - p1.longitude) * (2 + sinf(degreesToRadians(p1.latitude)) + sinf(degreesToRadians(p2.latitude)));
}
area = area * kEarthRadius * kEarthRadius / 2;
}
return area;
}
- (NSArray *)coordinates {
NSMutableArray *points = [NSMutableArray arrayWithCapacity:self.pointCount];
for (int i = 0; i < self.pointCount; i++) {
MKMapPoint *point = &self.points[i];
[points addObject:[NSValue valueWithMKCoordinate:MKCoordinateForMapPoint(* point)]];
}
return points.copy;
}
double degreesToRadians(double radius) {
return radius * M_PI / 180;
}
@end
我错过了什么?
【问题讨论】:
-
循环中缺少
i == N-1和i+1 == 0(环绕)的最后一步。 -
@MartinR 谢谢,我会解决的。
-
@MartinR 你是对的。如果您发表评论作为答案,我会接受。
-
@Shmidt---我用你的公式来计算面积,但它似乎给了我错误的结果,如果你更新最终代码,那就太好了。我认为这里缺少一些东西。我需要这个在我的应用程序上工作。期待您的来信。
标签: objective-c ios7 mapkit mkpolygon