How to Stop Multiple Times Method Calling of Didupdatelocations() in iOS

How to stop multiple times method calling of didUpdateLocations() in ios

Add some restriction there. For timespan between locations and accuracy

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *newLocation = locations.lastObject;

NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
if (locationAge > 5.0) return;

if (newLocation.horizontalAccuracy < 0) return;

// Needed to filter cached and too old locations
//NSLog(@"Location updated to = %@", newLocation);
CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:_currentLocation.coordinate.latitude longitude:_currentLocation.coordinate.longitude];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
double distance = [loc1 distanceFromLocation:loc2];


if(distance > 20)
{
_currentLocation = newLocation;

//significant location update

}

//location updated

}

locationManager didUpdateLocations fires twice on device, only once on simulator

Location Manager delegate methods can be called very frequently and at any time.

You may however, apply following algorithm to safeguard yourself:

  1. Create a global bool say didFindLocation.
  2. Set didFindLocation to false when you call startUpdatingLocation.
  3. Inside delegate call back didUpdateLocations:, if didFindLocation was false, set didFindLocation to true and then call stopUpdatingLocation.

Hope this helps.

locationManager:didUpdateLocations: always be called several times

Probably it depends about the accuracy you set to the locationManager. You have 3 kinds o localization Cell Radio, WiFi Map, GPS. If you set best as accuracy the location manager will continue to check you position, if the location with better accuracy is out of the range of the distance filter the delegate method will be called again.

Swift: How can I make less didupdatelocation calls

The answers of Rajeshkumar and Basir doesn't change much for the battery power, their delegate method is still called the same number of times per minute.

If you want to have a more efficient behavior, try to change the values of distanceFilter, desiredAccuracy, and activityType (You can find all you need here)

manager.distanceFilter = 50 // distance changes you want to be informed about (in meters)
manager.desiredAccuracy = 10 // biggest approximation you tolerate (in meters)
manager.activityType = .automotiveNavigation // .automotiveNavigation will stop the updates when the device is not moving

Also if you only need the location of the user when it changes significantly, you can monitor significantLocationChanges, can also work when your app is killed.

How to wait for didUpdateLocation method to be called before calling another method?

do like

func signup() { 
self.getLocation()

}

call in here

   func locationManager(_ manager: CLLocationManager, didUpdateLocations  locations: [CLLocation]) {
manager.stoptUpdatingLocation() // initialy stop updation
let locValue:CLLocationCoordinate2D = manager.location!.coordinate

locationLat = locValue.latitude
locationLon = locValue.latitude
let point = PFGeoPoint(latitude:locationLat, longitude:locationLon)
...
newUser[PF_USER_LOCATION] = point
newUser.signUpInBackground(block: { (succeed, error) -> Void in

if let errorInSignup = error {
...
} else {
...
// call your main view
}
})
}

didUpdateLocations called every time when viewWillAppear for Significant Location Change

You can manually check the distance with last saved location. Try this.

var lastLocation: CLLocation?
public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

if let lastLocation = lastLocation, let newLocation = locations.last {
if (lastLocation.distance(from: newLocation) < manager.distanceFilter) {
return
}
}

print("locations \(locations)")
lastLocation = locations.last
}


Related Topics



Leave a reply



Submit