GMSMapView animateToCameraPosition zoom in - zoom out animation
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.
Animating GMSMapView
After a lot of struggle I managed to animate the GMSMapView: Here is the code for the reference:
mapView.camera = GMSCameraPosition.cameraWithLatitude(58.998400,longitude: 10.035604, zoom: 1)
CATransaction.begin()
CATransaction.setValue(2.0, forKey: kCATransactionAnimationDuration)
let city = GMSCameraPosition.cameraWithLatitude(58.998400,longitude: 10.035604, zoom: 15)
self.mapView.animateToCameraPosition(city)
CATransaction.commit()
Change the camera position of GMS Map View with animation
Try the below code. You may update it for latest swift syntax.
mapView.camera = GMSCameraPosition.cameraWithLatitude(58.998400,longitude: 10.035604, zoom: 1)
CATransaction.begin()
CATransaction.setValue(2.0, forKey: kCATransactionAnimationDuration)
let city = GMSCameraPosition.cameraWithLatitude(58.998400,longitude: 10.035604, zoom: 15)
self.mapView.animateToCameraPosition(city)
CATransaction.commit()
GMSMapView is not zooming in
I guess you are doing something wrong here. You said that
Originally the zoom level is at 4. When I set the coordinate and zoom level, it stays zoomed out despite my debugger saying the zoom level is 2.
Just to be clear, zoom level 4 is > zoom level 2. If you want your map to be zoomed in try using higher values i.e. around 15, 16 etc.
Hope it helps.
GMSMapView: center on coordinate and zoom out to reveal user location
To display the POI in the centre, and the user location at an edge of the map, you should derive a third location point mirroring the relative distance of user llc from the POI on the other side of the POI.
if we are allowed to ignore the earth's curvature this is simple vector addition
I.e.
loc3 = poiLoc + (poiLoc - userLoc)
= 2*poiLoc - userLoc;
Then your map rect is defined by the userLoc and loc3.
Map camera position changes sharply without animation
I think the problem with this is that calling setCamera
will snap to the new zoom level without animating - and so the following call to animateToCameraPosition
won't actually animate anything, as the camera is already at that position.
However, if you removed the call to setCamera
then you'd probably end up in an infinite loop, as the call to animateToCameraPosition
won't actually update the camera until some time later (as the animation plays), and so on the next iteration of the loop the markers would still all be visible.
What you need to do is calculate the final zoom level that you need, and then make a single call to animateToCameraPosition
to move the camera there.
I posted some code for calculating the centre and zoom level for a camera to fit a bounds here:
How to setRegion with google maps sdk for iOS?
So you could use something similar to calculate a camera position which would fit your markers to the screen.
Related Topics
iOS App Crashes on Phone But Works Fine on Simulator
Scaling Physics Bodies in Xcode Spritekit
Swift: Sort Array by Sort Descriptors
Why Clusterannotationformemberannotations in Mkmapview Is Not Called
Cgaffinetransformmakerotation with Negative M_Pi
Facebook Sdk Login Throws Error in Swift 2 iOS 9
What Is the Purpose of Firebase Appcheck
Save Uiimage Array in Nsuserdefaults
How to Get Only Keys from Firebase
Uitableviewcell Shows Incorrect Results from Document Folder
@Fetchrequest + Case-Insensitive Sorting - Swiftui & Coredata
Issues Reading Data from Firebase Database
How to Instantiate and Load a View Controller Before Segueing to It Using Swift
Updating Label Takes Too Long (Swift)
Navigationlink Inside Lazyvgrid Cycles All Entries on Back, Swiftui
Google Signin Cocoapods Deprecated
How to Create an Alert in a Swift File Model That Works for Various View Controller
Making the Map Zoom to User Location and Annotation (Swift 2)