google-maps - 如何沿用户在 Swift 中移动的方向旋转 GMSMarker?

标签 google-maps swift3 coordinates gmsmapview

我正在使用此代码。

 let marker = GMSMarker()
 marker.position = coordinates
 marker.tracksViewChanges = true     
 marker.icon = UIImage(named:"car")
 marker.appearAnimation = kGMSMarkerAnimationNone
 marker.map = mapView

位置管理器代码
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{

    let location = locations.last! as CLLocation

    if(checkingLocation == false)
    {
    let camera = GMSCameraPosition.camera(withLatitude: (location.coordinate.latitude), longitude: (location.coordinate.longitude), zoom: 16.0)
    oldLocationCenter = location
    marker.position = (locationManager.location?.coordinate)!
         self.mapView?.animate(to: camera)
      //  checkingLocation = true
        locationManager.stopUpdatingLocation()

    }
    else
    {

        let updateCam = GMSCameraUpdate.setTarget(CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude))
        updateMarker(coordinates: location.coordinate, degrees:DegreeBearing(A: oldLocationCenter, B: location) , duration: 10.0)
        self.mapView?.animate(with: updateCam)
        locationManager.stopUpdatingLocation()

    }


}

func updateMarker(coordinates: CLLocationCoordinate2D, degrees: CLLocationDegrees, duration: Double){
    // Keep Rotation Short
    CATransaction.begin()
    CATransaction.setAnimationDuration(10.0)
    marker.rotation = degrees
    CATransaction.commit()

    // Movement
    CATransaction.begin()
    CATransaction.setAnimationDuration(duration)
    marker.position = coordinates

    // Center Map View
    let camera = GMSCameraUpdate.setTarget(coordinates)
    mapView.animate(with: camera)

    CATransaction.commit()
}

func DegreeBearing(A:CLLocation,B:CLLocation)-> Double{


    var dlon = self.ToRad(degrees: B.coordinate.longitude - A.coordinate.longitude)

    let dPhi = log(tan(self.ToRad(degrees: B.coordinate.latitude) / 2 + M_PI / 4) / tan(self.ToRad(degrees: A.coordinate.latitude) / 2 + M_PI / 4))

    if  abs(dlon) > M_PI{
        dlon = (dlon > 0) ? (dlon - 2*M_PI) : (2*M_PI + dlon)
    }
    return self.ToBearing(radians: atan2(dlon, dPhi))
}

func ToRad(degrees:Double) -> Double{
    return degrees*(M_PI/180)
}

func ToBearing(radians:Double)-> Double{
    return (ToDegrees(radians: radians) + 360) / 360
}

func ToDegrees(radians:Double)->Double{
    return radians * 180 / M_PI
}

通过使用上面的代码,我的标记(汽车)从旧位置移动到新位置,我也使用这些位置来获取方位角。但它不是旋转的。有没有其他方法可以实现这一目标?请指导我。

最佳答案

我通过更新标题找到了解决方案。
在 didUpdateLocations 方法中,我们需要更新标题

locationManager.startUpdatingHeading()

所以标记会在用户移动的地方旋转。
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading)
        {
            let  heading:Double = newHeading.trueHeading;
            marker.groundAnchor = CGPoint(x: 0.5, y: 0.5)
            marker.rotation = heading
            marker.map = mapView;
            print(marker.rotation)
        }

关于google-maps - 如何沿用户在 Swift 中移动的方向旋转 GMSMarker?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40932523/

相关文章:

ios - GMSMarker 信息窗口内容(片段)未更新

swift - `open` 类的初始化程序是否也需要打开?

swift3 - swift 3.0 TViOS 10.0 MultipeerConnectivity 有效,但有错误

math - 点相对于图像坐标的旋转

javascript - 谷歌地图不应用样式

javascript - 谷歌地图不显示任何标记

ios - 将选定的行数据传递给下一个 View Controller Swift 3

c++ - 我如何将经/纬度坐标转换为地球表面的一些 N-E 米距离?

image - 在 Matlab 中查找线的 x,y 坐标(来自二值图像)

android - 从 map fragment 切换到另一个 fragment 并返回