Ich versuche, eine Bereich Berechnung Kategorie für MKPolygon zu machen. Ich fand einige JS-Code https://github.com/mapbox/geojson-area/blob/master/index.js#L1 mit einem Link zum Algorithmus: http://trs-new.jpl.nasa.gov/dspace/handle/2014/40409. Dort heißt es: MKPolygon Bereich Berechnung
Hier ist mein Code, die ein falsches Ergebnis gab (in Tausend Mal mehr als tatsächliche):
#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
Was habe ich verpasst?
Der letzte Schritt für "i == N-1" und "i + 1 == 0" (Umbruch) fehlt in Ihrer Schleife. –
@MartinR danke, ich werde es beheben. – Shmidt
@MartinR Sie hatten Recht. Wenn Sie Ihren Kommentar als Antwort posten, werde ich ihn akzeptieren. – Shmidt