4

Created marker like below based on latitudes and longitudes help me to move marker to another location with animation.

GMSMarker *marker1 = [[GMSMarker alloc] init];
marker1.position = CLLocationCoordinate2DMake(12.9716, 77.5946);
marker1.title = @"Bangalore";
marker1.groundAnchor = CGPointMake(0.2, 0.9);
marker1.appearAnimation = kGMSMarkerAnimationPop;
marker1.icon = [UIImage imageNamed:@"Flag Filled -50.png"];
marker1.snippet = @"India";
marker1.map = _mapView;
Kumar Swamy
  • 772
  • 8
  • 23
  • possible duplicate - http://stackoverflow.com/questions/19115293/how-to-smoothly-move-gmsmarker-along-coordinates-in-objective-c – adjuremods Apr 06 '16 at 02:34

2 Answers2

7

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 :

Yash Bedi
  • 1,323
  • 17
  • 25
0

You can use setRotation method of GMSMarker.Hope it will work.