像 Uber iOS 应用程序一样在 Google 地图上移动标记?

Cod*_*e92 1 google-maps ios swift

我正在尝试开发一个基于 Uber 和 Ola 类似概念的应用程序。为此,我需要将 Google 地图集成到 iOS 上的用户跟踪位置。所以请告诉我如何使用 Google 地图在 iOS 中实现移动标记(汽车)动画。

Mru*_*ank 5

我有相同的应用程序,我需要向客户显示司机的位置以进行实时跟踪。需要做以下几件事。

  1. 不断获取驾驶员的新位置。
  2. 用动画更新驾驶员标记。

这是我用动画更新标记的代码。

func setDriversNewLocation(location:CLLocation) {
    CATransaction.begin()
    CATransaction.setAnimationDuration(timer.timeInterval)
    driverMarker.position = location.coordinate
    CATransaction.commit()
}
Run Code Online (Sandbox Code Playgroud)

其中timer.timeInterval与我调用 API 来获取驱动程序的新位置相同。即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!
    }
}
Run Code Online (Sandbox Code Playgroud)

在这里我获取当前用户的位置。但您可以根据您的要求更改纬度、经度和旋转角度,而不是根据您的要求。