MKPolygon面积计算 [英] MKPolygon area calculation

查看:196
本文介绍了MKPolygon面积计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为MKPolygon制作区域计算类别。
我发现了一些JS代码 https:// github。 com / mapbox / geojson-area / blob / master / index.js#L1 ,带有算法链接: http://trs-new.jpl.nasa.gov/dspace/handle/2014/40409
它说:

I'm trying to make an area calculation category for MKPolygon. I found some JS code https://github.com/mapbox/geojson-area/blob/master/index.js#L1 with a link to the algorithm: http://trs-new.jpl.nasa.gov/dspace/handle/2014/40409. It says:

这是我的代码,它给出了错误的结果(比实际数千倍):

Here is my code, which gave a wrong result (thousands times more than actual):

#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 和<$ c循环中缺少$ c> i + 1 = 0 (环绕)。

这篇关于MKPolygon面积计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆