Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GMSMapView animateToCameraPosition zoom in - zoom out animation

I am using Google maps services in iOS (Swift) and Android. In android, the map view has a method called animatreCamera that has an animation in which the movement has a "zoom out - zoom in" effect (if both cameras have the same zoom, the map view will zoom out the first part of the movement and then zoom in the second part). I want to achieve this effect with the GMSMapView in iOS, I have tried te following methods: animateToCameraPosition, animateToLocation, animateWithCameraUpdate, moveCamera and setting the camera by mapView.camera = GMSCameraPosition(target: location, zoom: 15, bearing: 0, viewingAngle: 0) and none of them have this animation. If possible, how can I achieve this animation when moving the camera?

like image 701
Oscar Vasquez Avatar asked Jun 17 '15 17:06

Oscar Vasquez


2 Answers

I think there is no direct way you can archive the same animation in the Google Maps iOS SDK.

A workaround can use iOS's dispatch_after method, first you can define a method to delay how many seconds you want:

func delay(#seconds: Double, completion:()->()) {
    let popTime = dispatch_time(DISPATCH_TIME_NOW, Int64( Double(NSEC_PER_SEC) * seconds ))

    dispatch_after(popTime, dispatch_get_main_queue()) {
        completion()
    }
}

Then you can zoom out your camera, move to a location, then zoom in with the delay method:

delay(seconds: 0.5) { () -> () in
    var zoomOut = GMSCameraUpdate.zoomTo(kGMSMinZoomLevel)
    mapView.animateWithCameraUpdate(zoomOut)

    delay(seconds: 0.5, { () -> () in
        var vancouver = CLLocationCoordinate2DMake(49.26,-123.11)
        var vancouverCam = GMSCameraUpdate.setTarget(vancouver)
        mapView.animateWithCameraUpdate(vancouverCam)

        delay(seconds: 0.5, { () -> () in
            var zoomIn = GMSCameraUpdate.zoomTo(kGMSMaxZoomLevel)
            mapView.animateWithCameraUpdate(zoomIn)

        })
    })
}

You use your own zoom value, I use kGMSMinZoomLevel and kGMSMaxZoomLevel here.

like image 190
ztan Avatar answered Oct 18 '22 15:10

ztan


Swift 4:

   func delay(seconds: Double, closure: @escaping () -> ()) {
    DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {
        closure()
    }
  }

Then call it :

    delay(seconds: 0.5) { () -> () in
            let zoomOut = GMSCameraUpdate.zoom(to: 10)
            self.mapView.animate(with: zoomOut)

            self.delay(seconds: 0.5, closure: { () -> () in

            var vancouver = CLLocationCoordinate2DMake(49.26,-123.11)
            var vancouverCam = GMSCameraUpdate.setTarget(vancouver)
            self.mapView.animate(toLocation: vancouverCam)

            self.delay(seconds: 0.5, closure: { () -> () in
                 let zoomIn = GMSCameraUpdate.zoom(to: 15)
                 self.mapView.animate(with: zoomIn)

                })
            })
        }
like image 43
Arafin Russell Avatar answered Oct 18 '22 14:10

Arafin Russell