gpt4 book ai didi

ios - GMSMapView 更新相机位置,以便在导航期间始终显示朝向顶部的方向

转载 作者:搜寻专家 更新时间:2023-11-01 06:26:47 24 4
gpt4 key购买 nike

我已经使用轴承属性在 mapview 上旋转相机,但无法定位相机,以便它始终显示顶部的导航。

下面是谷歌地图应用程序的屏幕截图,它在导航过程中自动旋转,因此它始终显示顶部的路线。

enter image description here

下面是我的应用程序的屏幕截图,它始终显示任何方向的路线。

enter image description here

我使用下面的代码来旋转相机,但真的不知道如何获得所需的方位角,它总是显示在顶部方向。

            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()

最佳答案

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)
}

如果你想动态地从你的路径中获取方位,那么你必须根据你的需要获取当前路径的终点。

关于ios - GMSMapView 更新相机位置,以便在导航期间始终显示朝向顶部的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53262371/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com