I have a GMSMapView connected to my mapView variable. It is all set up and even puts a random polyline on the map for the user. Currently I have a "navigate" button but all it does is zoom in on the user. I would like to make it so once they hit "navigate" it will make the camera follow the user and not let them scroll away from their location (much like the Google Maps app). How can I make this possible? Thanks!
Asked
Active
Viewed 769 times
1
1 Answers
1
I'm assuming you've already gotten a CLLocationManager and have started updating user's location with it. Once that's done, you want to set the mapView's camera in the CLLocationManager's delegate method whenever the location changes.
Something along the lines of this:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = manager.location {
// Create camera with the user's new location
let camera = GMSCameraPosition.cameraWithLatitude(location.coordinates.latitude, longitude: location.coordinates.longitude, zoom: 17.0)
// Animate the map to the new camera position
mapView.animateToCameraPosition(camera)
}
}
Play around with the zoom parameters to get the result you desire.
Hope it helps.

Nishant Shrestha
- 32
- 3
-
so this method runs whenever it notices that the users location has changed? Does it only run when "manager.startUpdatingLocation()" has happened? Does it stop when "manager.stopUpdatingLocation()" gets called? – skyleguy Nov 22 '16 at 16:39
-
and in your opinion where would be a good place to first call startUpdatingLocation? – skyleguy Nov 22 '16 at 16:42
-
@skyleguy Yes. That particular delegate method gets called whenever the user's location changes. [Apple's docs] (https://developer.apple.com/reference/corelocation/cllocationmanagerdelegate) states that it gets called whenever there is new location data available. That _usually_ means when the user's location changes. The delegate method will stop when stopUpdatingLocation() is called. As for a good place to startUpdatingLocation, it largely depends on your use case. From your initial question, I think viewDidLoad might be a good place for you to do it. – Nishant Shrestha Nov 23 '16 at 00:21