我正在尝试开发基于Uber和Ola概念的应用程序。因此,我需要将Google地图集成到iOS上的用户跟踪位置。所以请告诉我如何使用谷歌地图在iOS中实现移动标记(汽车)动画。
答案 0 :(得分:3)
我有同样的应用程序,我需要向客户显示Driver的位置以进行实时跟踪。以下事情是必需的。
这是我的动画更新标记代码。
func setDriversNewLocation(location:CLLocation) {
CATransaction.begin()
CATransaction.setAnimationDuration(timer.timeInterval)
driverMarker.position = location.coordinate
CATransaction.commit()
}
其中timer.timeInterval
与我调用API以获取Driver的新位置相同。即5秒。
的 - 编辑 - 强>
在这里,我为您的要求编写了代码。我正在获取用户的位置并基于该更新标记。
let camera = GMSCameraPosition.camera(withLatitude: 22.2729, longitude: 70.7584, zoom: 18.0)
mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
mapView.delegate = self
view = mapView
myMarker = GMSMarker()
myMarker.position = CLLocationCoordinate2D(latitude: 22.2729, longitude: 70.7584)
myMarker.groundAnchor = CGPoint(x: 0.5, y: 0.5)
myMarker.title = "Sydney"
myMarker.snippet = "Australia"
myMarker.map = mapView
myMarker.icon = #imageLiteral(resourceName: "car")
...
extension ViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
CATransaction.begin()
CATransaction.setAnimationDuration(0.2)
myMarker.position = locationManager!.location!.coordinate
CATransaction.commit()
}
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
lastUserBearing = newHeading.trueHeading
myMarker.rotation = lastUserBearing! - (lastMapBearing ?? 0)
}
}
extension ViewController: GMSMapViewDelegate {
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
lastMapBearing = position.bearing
myMarker.rotation = (lastUserBearing ?? 0) - lastMapBearing!
}
}
这里我得到了当前用户的位置。但根据您的要求,您可以根据您的要求更改纬度,经度和旋转角度。
答案 1 :(得分:0)
使用CATransaction:
let animationDuration = 4.0 // in seconds
CATransaction.begin()
CATransaction.setValue(NSNumber(value: animationDuration as Float), forKey: kCATransactionAnimationDuration)
// set marker properties to animate here
marker.position = position.coordinate
marker.rotation = position.course
CATransaction.commit()