gpt4 book ai didi

ios - MKPolygon 面积计算 Swift

转载 作者:搜寻专家 更新时间:2023-10-31 19:38:32 31 4
gpt4 key购买 nike

我一直在尝试对 MKPolygon 进行计算,我已经点击了此处的几个链接并进行了相应的调整。我似乎无法正确计算平方米。如果需要,我可以提供更多信息

这是我的代码

func polygonArea() -> Double{
var area: Double = 0
var kEarthRadius:Double = 6378137
var coord: NSArray = self.coordinates()
if (coord.count > 2){
var p1, p2, p3 : CLLocationCoordinate2D

var lowerIndex, middleIndex, upperIndex: Int
for var i = 0; i < points.count - 1; i++ {
if (i == (points.count - 2)){
lowerIndex = points.count - 2
middleIndex = points.count - 1
upperIndex = 0
}else if (i == points.count - 1){
lowerIndex = points.count - 1
middleIndex = 0
upperIndex = 1;

}else{
lowerIndex = i
middleIndex = i + 1
upperIndex = i + 2
}
p1 = points[lowerIndex]
p2 = points[middleIndex]
p3 = points[upperIndex]
area += degreesToRadians(p2.longitude - p1.longitude) * (2 + sin(degreesToRadians(p1.latitude)) + sin(degreesToRadians(p2.latitude)))

}
area = area * kEarthRadius * kEarthRadius / 2

}
return area
measureLabel.text = "\(area)"
}

我专门点击了这个链接 MKPolygon area calculation

最佳答案

这是 Objective-C 版本。您可以在您的代码中毫无问题地使用它。

polygon on a sphere area 1 polygon on a sphere area 2

#define kEarthRadius 6378137
@implementation MKPolygon (AreaCalculation)

- (double) area {
double area = 0;
NSMutableArray *coords = [[self coordinates] mutableCopy];
[coords addObject:[coords firstObject]];

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;
}

编辑:已更新,以便计算也将线上的点视为多边形内部。

关于ios - MKPolygon 面积计算 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34214183/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com