所有尝试使坐标之间的Google Maps中的标记运动动画化的尝试都指向在Swift中使用以下代码段:
CATransaction.begin()
CATransaction.setAnimationDuration(duration)
marker.position = coordindates
CATransaction.commit()
作为示例,以下是获得最多投票的SO帖子: How to smoothly move GMSMarker along coordinates in Objective c
在原点和目的地坐标对之间进行动画设置时,此方法正常。但是,我希望从GMSPath中的起始坐标到结束坐标进行动画处理。遍历路径中的点时,唯一显示的动画是在最后两个坐标之间。标记仅出现在倒数第二个点,并动画到最后一个点。
这是我的ViewController代码。它正在接收路由的一段作为编码路径(为进行测试,第一个编码路径:“ ika〜Exi | vN | AaDzAyCTc @ N [lBeEvB_ExBkExBmEjBwDXo @”)。
代码正在GMSPath对象内部的所有存储坐标中进行迭代,并尝试使用上面发布的代码段进行动画处理。如前所述,它仅显示最后两点之间的动画。
我尝试将所有代码集放置在ViewDidLoad,ViewDidAppear和ViewWillAppear中。 ViewDidLoad将缩放级别保持在洲际。 ViewDidAppear和ViewWillAppear适当地进行了缩放,并导致了本文中提到的有关动画的问题。该代码目前在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
}
}
答案 0 :(得分:0)
找到了使用计时器的解决方案: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
}
}