4
votes

I'm having a problem where the map is automatically zoomed out when the user tap on the button that will start tracking the user's location change like what the Apple Map app does when you tap on the anchor button once.

I tried to set the region again before I set the tracking mode, but it did not work. I do notice one thing though. It only happens if the user does not change the zoom level, i.e., if a user pinch the map before they hit the button, the map's zoom level is retained.

I tried to imitate that in code but so far, I have no luck.

My question is simple: is there a way to set the default zoom level for the user tracking mode? If not, can I somehow mimic a user's pinch (perhaps a gesture recognizer action?) to force set the scope?

Thanks in advance!

Note: I've searched for the answer on Stackoverflow and only found answers like this.

1

1 Answers

1
votes

I was using user tracking mode and it zoomed in to close for my apps design. If I understand your question correctly this is what you're looking for.

 @IBAction func getUserLocationPressed(_ sender: AnyObject) {

 if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {

        let span = MKCoordinateSpanMake(1.0, 1.0)

        let location = CLLocationCoordinate2D(latitude: mapView.userLocation.coordinate.latitude, longitude: mapView.userLocation.coordinate.longitude)

        let coordinateRegion = MKCoordinateRegion(center: location, span: span)

        mapView.setRegion(coordinateRegion, animated: true)

    }

}