12

In the Google Maps app for iOS, when you tap the My Location button, the camera slowly pans to your current location from whatever location you are currently at.

Following the Google's developer documentation I have implemented a similar method like the one shown when a user taps a button:

- (void) animateToCameraPosition:   (GMSCameraPosition *)  cameraPosition;

This is similar to my code:

 GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude: lat
                                                        longitude: long zoom: 17];

[googleMapView animateToCameraPosition:camera];

It works, and the camera pans to whatever location I want based on the coordinates I specified. However, the panning is really fast, instantaneous in fact.

I want the camera to slowly pan from whatever location I'm at to the location specified, as demonstrated in Google Maps.

How can I achieve this? Thanks

Pangu
  • 3,721
  • 11
  • 53
  • 120

2 Answers2

18

Basically, you need to put CATransaction around your code.

[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat: YOUR_SPEED] forKey:kCATransactionAnimationDuration];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude: lat
                                                    longitude: long zoom: 17];
[googleMapView animateToCameraPosition: camera];
[CATransaction commit];

The [NSNumberWithFloat: YOUR_SPEED] will define the duration of your animation.

ztan
  • 6,861
  • 2
  • 24
  • 44
  • @ztan does this give same zoom effect as of android?i cant get the same effect – LC 웃 May 25 '16 at 13:01
  • @Anish웃 try with the kCAMediaTimingFunctionEaseInEaseOut animation. `CATransaction.setAnimationTimingFunction(CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut))` – vicente.fava Dec 22 '16 at 18:15
  • I am able to animate to location but getting glitches while animating... – jayant rawat Mar 06 '20 at 07:07
12

for swift 3

CATransaction.begin()
CATransaction.setValue(Int(YOUR_SPEED), forKey: kCATransactionAnimationDuration)
// YOUR CODE IN HERE
CATransaction.commit()