我正在尝试在Google Maps iOS sdk中制作一个应用程序,该应用程序具有将汽车从一个位置移动到另一个位置的动画,并希望在移动汽车时找到正确的角度。
这是我到目前为止编写的代码:
func spawnRandomCarAround(location:CLLocation) {
let range:Double = 0.002
let carSpeed:Double = 0.0002
let randomPointX:Double = Double.random(in: -range...range)
let randomPointY:Double = Double.random(in: -range...range)
let latitude = location.coordinate.latitude + randomPointX
let longitude = location.coordinate.longitude + randomPointY
let carLocation = CLLocation(latitude: latitude, longitude: longitude)
let carMarker = GMSMarker(position: carLocation.coordinate)
carMarker.isFlat = true
carMarker.map = self.mapView
Timer.scheduledTimer(withTimeInterval: 3.0, repeats: true) { (timer) in
let newRandomPointX:Double = Double.random(in: -carSpeed...carSpeed)
let newRandomPointY:Double = Double.random(in: -carSpeed...carSpeed)
let newLatitude = carLocation.coordinate.latitude + newRandomPointX
let newLongitude = carLocation.coordinate.longitude + newRandomPointY
carMarker.rotation = Double.random(in: 0...360) //here I want to find the correct rotation.
CATransaction.begin()
CATransaction.setAnimationDuration(2.0)
carMarker.position = CLLocationCoordinate2D(latitude: newLatitude, longitude: newLongitude)
CATransaction.commit()
}
}
我相信在CALayer中工作可能是相同的。因此,任何答案将不胜感激。