GMSMapView animateToCameraPosition zoom in - zoom out animation

前端 未结 2 791
-上瘾入骨i
-上瘾入骨i 2021-01-12 08:08

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 movem

相关标签:
2条回答
  • 2021-01-12 08:40

    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)
    
                    })
                })
            }
    
    0 讨论(0)
  • 2021-01-12 08:54

    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.

    0 讨论(0)
提交回复
热议问题