Let me explain myself. I have created a Map where I show some locations (pins) and the user location. When I run the App, it opens and zooms in (span), then I want to move through the map, but the App "drags" the view back to the user location (it even zooms in back to normal state). My question is, how can I stop my App from doing that over and over again? because it is kind of annoying. Thanks in advance.
The function I used for this Span process was the following...
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
let span: MKCoordinateSpan = MKCoordinateSpanMake(1.0, 1.0)
let ubicacionUsuario: CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region: MKCoordinateRegion = MKCoordinateRegionMake(ubicacionUsuario, span)
mapView.setRegion(region, animated: true)
self.mapView.showsUserLocation = true
}
Inside of your didUpdateLocations function, you can create a counter. Essentially, what the counter would do is update and refresh the user's location on the map for a specific number of iterations. After that, you can use the .stopUpdatingLocation
method of the CLLocationManager
. So, for your case, you would have something like below:
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
// Top level class stuff, create CLLocationManager Instance
var manager = CLLocationManager()
// set initial value of counter to 0
var updateCount = 0
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//If the location has updated less than 3 times, continue to update the location
if updateCount < 3 {
let region: MKCoordinateRegion = MKCoordinateRegionMake(ubicacionUsuario, span)
mapView.setRegion(region, animated: true)
updateCount += 1
} else {
// Once the update has occurred a satisfactory number of times, prevent the update from automaitcally occuring again
manager.stopUpdatingLocation()
}
}
}