我在iOS(Swift)和Android中使用谷歌地图服务。在android中,地图视图有一个名为animatreCamera
的方法,该方法有一个动画,其中的动作有一个"缩小 - 放大"效果(如果两个相机具有相同的缩放,地图视图将缩小移动的第一部分,然后放大第二部分)。我希望通过iOS中的GMSMapView
实现此效果,我尝试了以下方法:animateToCameraPosition
,animateToLocation
,animateWithCameraUpdate
,moveCamera
并设置相机由mapView.camera = GMSCameraPosition(target: location, zoom: 15, bearing: 0, viewingAngle: 0)
并且他们都没有这个动画。如果可能,在移动相机时如何实现此动画?
答案 0 :(得分:4)
我认为没有直接的方法可以在Google Maps iOS SDK中存档相同的动画。
解决方法可以使用iOS的dispatch_after
方法,首先您可以定义一种方法来延迟您想要的秒数:
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()
}
}
然后您可以缩小相机,移动到某个位置,然后使用delay
方法放大:
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)
})
})
}
您使用自己的缩放值,我在这里使用kGMSMinZoomLevel
和kGMSMaxZoomLevel
。
答案 1 :(得分:1)
func delay(seconds: Double, closure: @escaping () -> ()) {
DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {
closure()
}
}
然后叫它:
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)
})
})
}