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

Ios 为什么这个函数一直循环?,ios,swift,logic,mapkit,Ios,Swift,Logic,Mapkit,尽管我从未编写过任何循环代码,但我的视图控制器中的上述函数仍在继续运行,有人能帮助发现错误的逻辑吗。下面是视图控制器的其余部分 func centerMapOnLocation(location: CLLocation) { let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, regionRadius * 2.0, regionRadius * 2.0) Ma

尽管我从未编写过任何循环代码,但我的视图控制器中的上述函数仍在继续运行,有人能帮助发现错误的逻辑吗。下面是视图控制器的其余部分

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

}

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

didUpdateLocations
在每次位置更新时更新。您已经调用了该函数,这就是为什么它调用了轻微的位置更改。您还可以公开一些UI,允许用户控制
setUserTrackingMode:animated:
设置,MapView将为您的
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.
}