Reputation: 3663
this is my locationUpdate
delegate
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()
}
I call to this in viewDidLoad
in this way.
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate=self
self.locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters
self.locationManager.requestAlwaysAuthorization()
self.locationManager.startUpdatingLocation()
}
My problem is it seems locationmanager doesn't stop updating. My getLocationData()
method get fire 2 times.
Upvotes: 1
Views: 839
Reputation: 777
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()
}
this way you ensure self.getLocationData() won't get called unless accuracy is less than 100 and it would get called once
Upvotes: 2