MapKit围绕中心旋转矩形

时间:2018-03-15 22:35:54

标签: ios geometry mapkit rectangles

我需要围绕它旋转矩形的中心,矩形是地图套件上的多边形,我有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)
    }
}

在哪里"让旋转"可以是0到180。 我有下一个结果

enter image description here

enter image description here

正如你所看到的,矩形变成了钻石,角度不是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`

希望得到帮助!

1 个答案:

答案 0 :(得分:0)

我发现了什么是错的,公式错了,因为地球并不平坦。现在它正常运作。 代码:

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