MapKit 围绕中心旋转矩形

MapKit Rotate Rectangle Around Center

我需要围绕它的中心旋转矩形,矩形是地图工具包上的多边形,我有 4 个点和中心。我分别旋转每个点,然后创建新的多边形。

我使用这个代码:

    if let rotation = rotation, let center = zoneCenter {
        let radians = Double(rotation) * (Double.pi/180.0)
        print(radians)
        var newPoints: [CLLocationCoordinate2D] = []
        for point in squarePoints {
            let latitude: CLLocationDegrees = center.latitude + (point.longitude - center.longitude) * sin(radians) + (point.latitude - center.latitude) * cos(radians)

            let longitude: CLLocationDegrees = center.longitude + (point.longitude - center.longitude) * cos(radians) - (point.latitude - center.latitude) * sin(radians)

            newPoints.append(CLLocationCoordinate2DMake(latitude, longitude))
        }

        squarePointsWithRotation = newPoints
        squareOverlay = MKPolygon(coordinates: &newPoints, count: squarePoints.count)
        mapView.add(squareOverlay)
    }
}

其中 "let rotation" 可以是 0 到 180。 我有下一个结果

如您所见,矩形变成了菱形,并且角度不是必须的 90 度。无法弄清楚如何使所有角度保持 90 度。 我使用这个公式进行旋转

/// X = x0 + (x - x0) * cos(a) - (y - y0) * sin(a);
/// Y = y0 + (y - y0) * cos(a) + (x - x0) * sin(a);
/// where x0, y0 - center, a - rotation angle, x, y  - point to rotate`

希望得到帮助!

我发现问题了,公式错了,因为地球不是平的。现在它工作正常。 代码:

let latitude: CLLocationDegrees = center.latitude + sin(radians) * (point.longitude - center.longitude) * abs(cos(center.latitude * (Double.pi/180.0))) + cos(radians) * (point.latitude - center.latitude)

let longitude: CLLocationDegrees = center.longitude + cos(radians) * (point.longitude - center.longitude) - sin(radians) * (point.latitude - center.latitude) / abs(cos((center.latitude * (Double.pi/180.0))))

和公式:

/// X = x0 + cos(a) * (x - x0) - sin(a) * (y - y0) / abs(cos(y0 * pi/180));
/// Y = y0 + cos(a) * (y - y0) + sin(a) * (x - x0) * abs(cos(y0 * pi/180));
/// where x0, y0 - center, a - rotation angle, x, y  - point to rotate