【问题标题】:How to get area for MKPolygon in iOS如何在 iOS 中获取 MKPolygon 的区域
【发布时间】:2013-04-08 13:46:55
【问题描述】:

如何在 iOS 中获取 MKPolygon 或 MKOverlay 的面积?

我已经能够将多边形分解为三角形并进行一些数学运算来获得面积。但是,不适用于不规则多边形。

我正在考虑在这里做类似“更复杂的案例”的事情:http://www.mathopenref.com/coordpolygonarea2.html

我希望 MapKit 有一个更简单的解决方案。

谢谢, 蒂姆

【问题讨论】:

    标签: ios overlay mapkit polygon


    【解决方案1】:

    这是我正在使用的实现。

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

    在 Swift 3 中:

    let kEarthRadius = 6378137.0
    
    extension MKPolygon {
        func degreesToRadians(_ radius: Double) -> Double {
            return radius * .pi / 180.0
        }
    
        func area() -> Double {
            var area: Double = 0
    
            var coords = self.coordinates()
            coords.append(coords.first!)
    
            if (coords.count > 2) {
                var p1: CLLocationCoordinate2D, p2: CLLocationCoordinate2D
                for i in 0..<coords.count-1 {
                    p1 = coords[i]
                    p2 = coords[i+1]
                    area += degreesToRadians(p2.longitude - p1.longitude) * (2 + sin(degreesToRadians(p1.latitude)) + sin(degreesToRadians(p2.latitude)))
                }
                area = abs(area * kEarthRadius * kEarthRadius / 2)
            }
    
            return area
        }
    
        func coordinates() -> [CLLocationCoordinate2D] {
            var points: [CLLocationCoordinate2D] = []
            for i in 0..<self.pointCount {
                let point = self.points()[i]
                points.append(MKCoordinateForMapPoint(point))
            }
            return Array(points)
        }
    }
    

    【讨论】:

    • @spikerola swift 中的 sin 函数是否像 Objective-c 中的 sinf 一样?
    • 是的,现在它适用于 Doubles 而不是 swift 3 中的 Floats。
    【解决方案2】:

    我通过在多边形中的点做一个小循环来解决这个问题。对于每 3 个点,我检查该三角形的中心是否在多边形中。如果是继续,如果不是,则连接多边形,使多边形中没有倾角。完成后,获取多边形中的三角形并进行数学运算以获取面积。然后减去被移除的三角形。

    希望这对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2013-01-06
      • 2011-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多