【问题标题】:How to rotate GMSMarker in the direction which user is moving in Swift?如何沿用户在 Swift 中移动的方向旋转 GMSMarker?
【发布时间】:2016-12-02 12:43:30
【问题描述】:

我正在使用此代码。

 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
}

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

【问题讨论】:

  • 这可以通过位置管理器从您旋转汽车的位置获取方位角并使用真实位置向前移动您的标记。
  • @chiragshah.. 是的,我想移动我的位置,但是当我的位置更新时,汽车没有移动。它在跳跃
  • 您可以发布您的位置管理器代码吗?
  • 您为位置管理器使用的过滤器使用“最佳”

标签: google-maps swift3 coordinates gmsmapview


【解决方案1】:

我通过更新标题找到了解决方案。 在 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)
        }

【讨论】:

  • 航向可能在某些设备中不可用,因此请确保在开始航向前检查。还有一件事可能需要更多的电池电量,因此请在适当的管理下使用
  • @chiragshah..好的。如何检查航向是否可用。
  • 代码在目标 c if ([CLLocationManager headingAvailable]) { locManager.headingFilter = 5; [locManager startUpdatingHeading]; }
  • @chiragshah.. 有没有不使用标题的其他选项。
  • cllocation 它自己在 CLLocation 对象中给出标题(如果可用)
【解决方案2】:

首先要正确导航,您需要像这样设置位置管理器对象

fileprivate func initLocationManager()
    {
        locationManager = CLLocationManager();
        locationManager!.delegate = self;
        locationManager!.desiredAccuracy = kCLLocationAccuracyBest;
        locationManager!.activityType = .automotiveNavigation;
        if LocationTracker.authorized() == false
        {
            requestAuthorization()
        }
    }

设置后,您需要找到合法位置,因为在 ios 系统中尝试提供最佳位置但不确定因此某些时间位置也来自缓存,或者您的位置可能是 zick zack,因此您需要在 locationUpdate 中使用以下代码

 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    {
        if locations.count > 0
        {
            let _  = locations.first
            let location = locations[locations.count-1]

            let maxAge:TimeInterval = 60;
            let requiredAccuracy:CLLocationAccuracy = 100;

            let locationIsValid:Bool = Date().timeIntervalSince(location.timestamp) < maxAge && location.horizontalAccuracy <= requiredAccuracy;
            if locationIsValid
            {
                NSLog(",,, location : %@",location);


                NSLog("valid locations.....");

            }
        }
    }

您的导航顺利,但要注意一点,iPad 的定位精度不好,这种类型的模块只适用于 iPhone,祝你好运

【讨论】:

  • 感谢您的回复。我会努力的。
  • @UmaMadhavi 我很久以前就问过同样的问题,这里是链接stackoverflow.com/questions/36450526/… 看看一次
  • ..我看过你的问题。但我正在使用 swift。你得到确切的解决方案了吗..
  • 如何在追踪应用中显示旋转。我只从服务器获取纬度和经度。
  • 你需要找到两个位置之间的标题
猜你喜欢
  • 2016-07-26
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 2013-01-04
  • 2017-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多