Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS GoogleMaps SDK - animateToCameraPosition animation finished handler?

Currently I am using the GoogleMaps SDK for iOS for various operations. When calling

[self.googleMapsView animateToCameraPosition:[GMSCameraPosition 
                            cameraWithLatitude:LATITUDE
                                     longitude:LONGITUDE
                                          zoom:ZOOM]];

is there a completion handler to determine wether the animation finished or not?

Of course I get with the GMSMapViewDelegate updates about the cameraPosition but how should I check if the animation finished?

- (void)mapView:(GMSMapView *)mapView 
didChangeCameraPosition:(GMSCameraPosition *)position;
like image 434
Robert Weindl Avatar asked Mar 04 '13 14:03

Robert Weindl


3 Answers

For the reference of future readers of this post, Google Maps SDK for iOS Version 1.4.0 released in July 2013 has added a new delegate method mapView:idleAtCameraPosition: which will be fired at the end of any camera movement - be it programatic animation like in this question or user triggered movements.

like image 98
tony m Avatar answered Nov 18 '22 18:11

tony m


This might work (I haven't tried it):

[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat: 1.0f] forKey:kCATransactionAnimationDuration];
[self.googleMapsView animateToCameraPosition:[GMSCameraPosition 
                        cameraWithLatitude:LATITUDE
                                 longitude:LONGITUDE
                                      zoom:ZOOM]];
[CATransaction setCompletionBlock:^{
    // ... whatever you want to do when the animation is complete
}];
[CATransaction commit];

Basically, this creates an animation transaction that animates your camera movement (change the value for numberWithFloat: to change the speed) and you set your own completion block stating what you want to do when the animation is over. [CATransaction commit] is what fires the animation off.

Note: this answer partially based off this answer.

like image 33
Bryce Thomas Avatar answered Nov 18 '22 17:11

Bryce Thomas


I came across the issue of google's animation methods lacking completion handlers recently.
The best solution I've found so far is to attach my own completion handler via CATransation API.

private func attachCompletionHandlerToGoogleAnimations(@noescape animations: () -> Void, #completion: (() -> Void)!) {
    CATransaction.begin()
    CATransaction.setCompletionBlock(completion)
    animations()
    CATransaction.commit()
}

Example usage:

attachCompletionHandlerToGoogleAnimations({
    googleMapView.animateToLocation(coordinate)
}) {
    println("camera moved to location \(coordinate)")
}
like image 3
Nikita Kukushkin Avatar answered Nov 18 '22 18:11

Nikita Kukushkin