Using Xcode 9 Swift 4 : Moving markers smoothly.
@objc func moveMarker(){
self.lat += 0.0017
CATransaction.begin()
CATransaction.setValue(2.0, forKey: kCATransactionAnimationDuration)
CATransaction.setCompletionBlock {
self.marker.groundAnchor = CGPoint(x: 0.5, y: 0.5)
}
self.mapView.animate(to: GMSCameraPosition.camera(withLatitude: self.lat, longitude: self.lon, zoom: 15))
self.marker.position = CLLocationCoordinate2D(latitude: self.lat, longitude: self.lon)
CATransaction.commit()
self.marker.map = self.mapView
}
Your Marker will move on Map like Uber's Car on map.
It is not necessary to mention .groundAnchor
property, Read Google Docs for that.
Also, Update the self.lat
and self.lon
before passing the values in CATransaction
. ( lat
and lon
are Global variable with default value)
Lastly I've used Timer.scheduledTimer
to call
override func viewDidAppear(_ animated: Bool) {
var timer = Timer()
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(moveMarker), userInfo: nil, repeats: true)
}
I bet it'll will help some one. =)
Result :
