Ios GMSMapView更新相机位置,以便在导航过程中始终显示朝向顶部的方向
我已使用“方向角”属性在mapview上旋转摄影机,但无法定位摄影机,使其始终显示朝向顶部的导航 下面是谷歌地图应用程序的屏幕截图,它在导航过程中自动旋转,以便始终显示朝向顶部的路线 下面是我的应用程序的屏幕截图,它总是显示任何方向的路线,如 我使用了下面的代码来旋转相机,但我真的不知道如何获得所需的方位角s,它将始终显示在顶部方向Ios GMSMapView更新相机位置,以便在导航过程中始终显示朝向顶部的方向,ios,swift,google-maps,gmsmapview,bearing,Ios,Swift,Google Maps,Gmsmapview,Bearing,我已使用“方向角”属性在mapview上旋转摄影机,但无法定位摄影机,使其始终显示朝向顶部的导航 下面是谷歌地图应用程序的屏幕截图,它在导航过程中自动旋转,以便始终显示朝向顶部的路线 下面是我的应用程序的屏幕截图,它总是显示任何方向的路线,如 我使用了下面的代码来旋转相机,但我真的不知道如何获得所需的方位角s,它将始终显示在顶部方向 let cameraPosition = GMSCameraPosition.camera(withTarget: currentLoc
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()
使用下面的轴承计算方法,您将获得所需的起点到终点的轴承
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)
}
如果你想动态地从你的路径中获得方向,那么你必须根据你的需要得到当前路线的终点
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)
}