Search code examples
iosswiftcllocation

I have the map set to rotate to the users location. How do I allow the user to interact with the map?


I have the map set to center and rotate to the user's location and direction but the map view is locked, as in, I can not interact with it.

extension MapScreen: CLLocationManagerDelegate {
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let location = locations.last else { return }
        let region = MKCoordinateRegion.init(center: location.coordinate, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
        mapView.setRegion(region, animated: true)


    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        checkLocationAuthorization()

    func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
        mapView.camera.heading = newHeading.magneticHeading
        mapView.setCamera(mapView.camera, animated: true)
            }
        }
    }
}

How do I allow the user to explore his surroundings; scroll, zoom, interact with pins, etcetera, while centering and rotating to the user?


Solution

  • By using the didUpdateLocations delegate method you are continually updating the map view, overriding any changes made by the user.

    The solution is not to use CLLocationManager delegate methods to update the map view.

    Simply set userTrackingMode on the map to .followWithHeading and the map view will do the work for you. As soon as the user interacts with the map the tracking mode will change to .none.

    You can add a button to re-activate the .followWithHeading user tracking mode.