这是我的代码
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
#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全部内容,希望文章能够帮你解决ios – MKPolygon区域计算Swift所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)