Swift - GMSMarker 从 GMSPath 沿 CLCoordinates 数组移动(Google Maps SDK for iOS)

Swift - Movement of GMSMarker along array of CLCoordinates from GMSPath (Google Maps SDK for iOS)

所有尝试在 Google 中对标记移动进行动画处理 坐标之间的地图指向在 Swift 中使用以下代码片段:

CATransaction.begin()
CATransaction.setAnimationDuration(duration)
marker.position = coordindates
CATransaction.commit()

这里的例子是得票最多的 SO post: How to smoothly move GMSMarker along coordinates in Objective c

在原点和目标坐标对之间制作动画时,这可以正常工作。但是,我正在寻找从 GMSPath 中的起始坐标到结束坐标的动画。遍历路径中的点时,唯一显示的动画位于最后两个坐标之间。标记仅出现在倒数第二个点并动画到最后一个点。

这是我的 ViewController 代码。它正在接收一段路由作为编码路径(用于测试,第一个编码路径:"ika~Exi|vN|AaDzAyCTc@N[lBeEvB_ExBkExBmEjBwDXo@")。

代码正在遍历 GMSPath 对象内所有存储的坐标,并尝试使用上面的代码段 post 制作动画。如前所述,它只显示最后两点之间的动画。

我已经尝试将所有代码集放在 ViewDidLoad、ViewDidAppear 和 ViewWillAppear 中。 ViewDidLoad 将缩放级别保持在洲际。 ViewDidAppear 和 ViewWillAppear 适当地进行了缩放,并导致了此 post 中提到的围绕动画的问题。该代码目前在 ViewDidAppear 和 ViewWillAppear 之间拆分,但如果单独放在任一方法中,它们的行为将相同。

import UIKit
import GoogleMaps
import CoreLocation

class MapVC:UIViewController {

    var mapView:GMSMapView?

    var polyline:GMSPolyline?

    var path:GMSPath?

    var encodedPath:String? = nil

    var marker:GMSMarker?

    override func viewDidLoad() {
        super.viewDidLoad()


        setupMap()

    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        if encodedPath != nil {

            self.path = GMSPath(fromEncodedPath: encodedPath!)

            self.polyline = GMSPolyline(path: path)
            self.polyline!.map = self.mapView!

            let bounds:GMSCoordinateBounds = GMSCoordinateBounds(path: path!)

            let update = GMSCameraUpdate.fit(bounds, withPadding: 10.0)
            self.mapView!.animate(with: update)



        } else {

            print("nil path")

        }
        let a=2

    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        var index:UInt = 0

        let count:UInt = self.path!.count()

        if count > 0 {

            marker = GMSMarker(position: self.path!.coordinate(at:index))
            marker!.map = self.mapView

            index += 1

            while index < count {

                CATransaction.begin()
                CATransaction.setAnimationDuration(30)
                self.marker!.position = self.path!.coordinate(at:index)
                CATransaction.commit()
                index += 1

            }

        }

    }




    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func setupMap() {

        let camera = GMSCameraPosition.camera(withLatitude: 36.5, longitude: -82.5, zoom: 16)
        mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)

        self.view = mapView

    }

}

找到了使用计时器的解决方案:https://github.com/antonyraphel/ARCarMovement/blob/master/ARCarMovementSwift/ARCarMovementSwift/ViewController.swift

来自上面显示的视图控制器的更新代码:

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        marker = GMSMarker(position: self.path!.coordinate(at:self.index))
        marker!.map = self.mapView

        timer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: 
                #selector(MapVC.timerTriggered), userInfo: nil, repeats: true)


    }


    @objc func timerTriggered() {

        if self.index < self.path!.count() {

            CATransaction.begin()
            CATransaction.setAnimationDuration(1.9)
            self.marker!.position = self.path!.coordinate(at:index)
            CATransaction.commit()
            self.index += 1

        } else {

            timer.invalidate()
            timer = nil

        }

    }