为什么这个函数一直在循环?

Why does this function keep looping?

func centerMapOnLocation(location: CLLocation) {
    let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
        regionRadius * 2.0, regionRadius * 2.0)
    MapOutlet.setRegion(coordinateRegion, animated: true)
}

我下面的视图控制器中上面的这个函数继续 运行 尽管我从来没有编写任何循环,有人可以帮助发现不正确的逻辑。这是视图控制器的其余部分

import UIKit
import MapKit
import CoreLocation


class ViewControllerPublic: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()



let initialLocation = CLLocation(latitude: 3.632488, longitude: -117.898886)
override func viewDidLoad() {
    super.viewDidLoad()
    centerMapOnLocation(initialLocation)

    // Ask for Authorisation from the User.
    self.locationManager.requestAlwaysAuthorization()

    // For use in foreground
    self.locationManager.requestWhenInUseAuthorization()

    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.startUpdatingLocation()
    }

}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let locValue:CLLocationCoordinate2D = manager.location!.coordinate
    print("locations = \(locValue.latitude) \(locValue.longitude)")
    let currentLocation = CLLocation(latitude: locValue.latitude, longitude: locValue.longitude)
    centerMapOnLocation(currentLocation)
}

let regionRadius: CLLocationDistance = 2300
func centerMapOnLocation(location: CLLocation) {
    let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
        regionRadius * 2.0, regionRadius * 2.0)
    MapOutlet.setRegion(coordinateRegion, animated: true)
}

@IBOutlet weak var MapOutlet: MKMapView!

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

你永远不会使 CLLocation manager 无效,所以正如@Ashish Kakkad 所说,只要你的位置发生最轻微的变化,你的函数就会再次被调用。如果您不想要这种行为,那么在您获得 didUpdateLocations 中的位置后,您需要执行 locationManager.stopUpdatingLocation()。或者,如果您确实希望您的应用程序在每次位置更改时更新地图,您可能需要考虑更改所需的位置精度。