13

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;
Saxon Druce
  • 17,406
  • 5
  • 50
  • 71
Robert Weindl
  • 1,092
  • 1
  • 10
  • 30
  • 2
    On related: didChangeCameraPosition also is called multiple (10+) times on simple swipe or pinch gestures instead of just being called once the map has been changed. – brendan Mar 12 '13 at 20:50
  • Im suffering with this because I'm updating a textfield whenever I call this delegate method with the new address retrieved from google maps API, and it's been called so many times that I get OVER_QUERY_LIMIT – David Cespedes Apr 04 '13 at 20:47

5 Answers5

19

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.

tony m
  • 4,769
  • 1
  • 21
  • 28
9

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.

Community
  • 1
  • 1
Bryce Thomas
  • 10,479
  • 26
  • 77
  • 126
3

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)")
}
Nikita Kukushkin
  • 14,648
  • 4
  • 37
  • 45
2

I don't believe there is, however...

A method that has worked well for me so far is to set a timer to fire (very) shortly after the location stops updating:

- (void)mapView:(GMSMapView*)mapView didChangeCameraPosition:(GMSCameraPosition*)position {
  // _panTimer is an instance variable of the delegate.
  [_panTimer invalidate];
  _panTimer = [NSTimer timerWithTimeInterval:0.2
                                      target:self
                                    selector:@selector(_mapHasStoppedMoving)
                                    userInfo:nil
                                     repeats:NO];
  [[NSRunLoop currentRunLoop] addTimer:_panTimer forMode:NSDefaultRunLoopMode];
}
andybons
  • 382
  • 1
  • 2
  • 10
  • but if the user is just making a small change? the timer wouldn't be invalidated and the mapHasStoppedMoving wouldn't be called – David Cespedes Apr 04 '13 at 21:26
  • Any change would cause the method to eventually be fired. The _panTimer is an ivar of the class that manages the GMSMapView, otherwise known as the mapView's delegate. – andybons Apr 05 '13 at 00:41
  • that's how I do it in 1.1.2. there is an enhancement request on the GMS issue tracker for a delegate method AFTER the change – Daij-Djan May 19 '13 at 09:01
1

SWIFT version example:

let vancouver = CLLocationCoordinate2D(latitude: 49.26, longitude: -123.11)
let calgary = CLLocationCoordinate2D(latitude: 51.05,longitude: -114.05)
let bounds = GMSCoordinateBounds(coordinate: vancouver, coordinate: calgary)
let cameraPosition = GMSCameraUpdate.fit(bounds)

CATransaction.begin()
CATransaction.setValue(1.0/*duration in seconds*/, forKey: kCATransactionAnimationDuration)
CATransaction.setCompletionBlock({
    print("animation complete, do whatever you want here")
})
mMapView.animate(with: cameraPosition)
CATransaction.commit()
lenooh
  • 10,364
  • 5
  • 58
  • 49