我已经使用了方位属性来在mapview上旋转相机,但无法定位相机,因此它始终将导航显示在顶部。
下面是google map应用的屏幕截图,该应用会在导航过程中自动自动旋转,以便始终显示指向顶部的路线。
下面是我的应用的屏幕截图,该屏幕始终显示任意方向的路线。
我使用下面的代码旋转摄像机,但实际上不知道如何获得始终在顶部显示的所需方位角。
let cameraPosition = GMSCameraPosition.camera(withTarget: currentLocation, zoom: self.camera.zoom, bearing: MyRide.shared.bearing, viewingAngle: 45)
let cameraUpdate = GMSCameraUpdate.setCamera(cameraPosition)
CATransaction.begin()
CATransaction.setValue(1.0, forKey: kCATransactionAnimationDuration)
self.animate(with: cameraUpdate)
CATransaction.commit()
答案 0 :(得分:2)
let camera = GMSCameraPosition.camera(withTarget: CLLocationCoordinate2DMake(startLat, startLong), zoom: 17, bearing: getBearingBetweenTwoPoints(point1: CLLocationCoordinate2DMake(startLat, startLong), point2:CLLocationCoordinate2DMake(endLat, endLong)), viewingAngle: 45)
使用下面的方位角计算方法,您将获得所需的起点到终点的方位角。
func degreesToRadians(degrees: Double) -> Double { return degrees * .pi / 180.0 }
func radiansToDegrees(radians: Double) -> Double { return radians * 180.0 / .pi }
func getBearingBetweenTwoPoints(point1 : CLLocationCoordinate2D, point2 : CLLocationCoordinate2D) -> Double {
let lat1 = degreesToRadians(degrees: point1.latitude)
let lon1 = degreesToRadians(degrees: point1.longitude)
let lat2 = degreesToRadians(degrees: point2.latitude)
let lon2 = degreesToRadians(degrees: point2.longitude)
let dLon = lon2 - lon1
let y = sin(dLon) * cos(lat2)
let x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon)
let radiansBearing = atan2(y, x)
return radiansToDegrees(radians: radiansBearing)
}
如果要动态获取路径的方位,则必须根据需要获取当前路线的终点。