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:
- Create a global
bool
saydidFindLocation
. - Set
didFindLocation
tofalse
when you callstartUpdatingLocation
. - Inside delegate call back
didUpdateLocations:
, ifdidFindLocation
wasfalse
, setdidFindLocation
totrue
and then callstopUpdatingLocation
.
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
Which iOS App Version/Build Number(S) Must Be Incremented Upon App Store Release
Save Images with Phimagemanager to Custom Album
iOS Nsdate() Returns Incorrect Time
Fix CSS Hover on Iphone/Ipad/Ipod
Uiscreen Mainscreen Bounds Returning Wrong Size
Uistackview Distribution Fill Equally
Autolayout - Make Height of View Relative to Half Superview Height
How to Set Adaptive Multiline Uilabel Text
Alertcontroller Is Not in the Window Hierarchy
Building for iOS Simulator, But the Linked Framework '****.Framework' Was Built for iOS
In iOS 12, When Does the Uicollectionview Layout Cells, Use Autolayout in Nib
How to Put an Image in a Realm Database
How to Password Protect Writing to Nfc Ntag216 Tag on iOS 13 Using Nfc Core
App Killed by Sigkill When Changing Privacy Settings
Set Default iOS Local Notification Style for Application
What Is Export * in Module.Modulemap File Inside Each Framework