这是我的locationUpdate
代表
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let lastLocation:CLLocation=locations.last!
let accuracy:CLLocationAccuracy=lastLocation.horizontalAccuracy// the radius of uncertainity location in meters
print("Received location \(lastLocation) with acccuracy \(accuracy)")
if (accuracy < 100.0)
{
let span:MKCoordinateSpan=MKCoordinateSpanMake(0.14,0.14)
let region:MKCoordinateRegion=MKCoordinateRegionMake(lastLocation.coordinate, span)
self.mapview.setRegion(region, animated: true)
manager.stopUpdatingLocation()
print("-------LOCATION UPDATE---------")
self.getLocationData()
}
我以这种方式在viewDidLoad
中打电话给我。
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate=self
self.locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters
self.locationManager.requestAlwaysAuthorization()
self.locationManager.startUpdatingLocation()
}
我的问题是,locationmanager似乎没有停止更新。我的getLocationData()
方法会被点燃2次。
答案 0 :(得分:2)
if locations.count > 0 {
manager.stopUpdatingLocation()
}
if (accuracy < 100.0)
{
let span:MKCoordinateSpan=MKCoordinateSpanMake(0.14,0.14)
let region:MKCoordinateRegion=MKCoordinateRegionMake(lastLocation.coordinate, span)
self.mapview.setRegion(region, animated: true)
print("-------LOCATION UPDATE---------")
self.getLocationData()
}
else {
manager.startUpdatingLocation()
}
这样你就可以确保不会调用self.getLocationData(),除非精度小于100并且它会被调用一次