【问题标题】:GMSMapView update camera position so that it will always show direction towards top during navigationGMSMapView 更新相机位置,以便在导航期间始终显示朝向顶部的方向
【发布时间】:2019-04-15 04:28:20
【问题描述】:

我使用轴承属性在地图视图上旋转相机,但无法定位相机,使其始终显示顶部导航。

下面是google map app的截图,它在导航时会自动旋转,这样它总是会显示顶部的路线。

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

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

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

【问题讨论】:

    标签: ios swift google-maps gmsmapview bearing


    【解决方案1】:
    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)
    }
    

    如果您想从路径动态获取方位,则必须根据需要获取当前路线的终点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-01
      • 1970-01-01
      • 2021-10-22
      • 2018-08-24
      • 1970-01-01
      • 1970-01-01
      • 2018-07-29
      相关资源
      最近更新 更多