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

前端 未结 10 1113
無奈伤痛
無奈伤痛 2020-11-28 06:45

This my code......

 -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
 {

    location_updated = [locations lastObj         


        
相关标签:
10条回答
  • 2020-11-28 07:14

    Write this method when ever you want to stop updating location manager

    [locationManager stopUpdatingLocation];
    
    0 讨论(0)
  • 2020-11-28 07:15

    You can use a static variable to store the latest location timestamp and then compare it to the newest one, like this:

    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
        [manager stopUpdatingLocation];
        static NSDate *previousLocationTimestamp;
    
        CLLocation *location = [locations lastObject];
        if (previousLocationTimestamp && [location.timestamp timeIntervalSinceDate:previousLocationTimestamp] < 2.0) {
            NSLog(@"didUpdateLocations GIVE UP");
            return;
        }
        previousLocationTimestamp = location.timestamp;
    
        NSLog(@"didUpdateLocations GOOD");
    
        // Do your code here
    }
    
    0 讨论(0)
  • 2020-11-28 07:18

    locationManager.startUpdatingLocation() fetch location continuously and didUpdateLocations method calls several times, Just set the value for locationManager.distanceFilter value before calling locationManager.startUpdatingLocation().

    As I set 200 meters(you can change as your requirement) working fine

        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.distanceFilter = 200
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
    
    0 讨论(0)
  • 2020-11-28 07:19

    you can write : [manager stopUpdatingLocation]; manager = nil; in didupdatelocation delegate

    0 讨论(0)
  • 2020-11-28 07:21

    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
    
    }
    
    0 讨论(0)
  • 2020-11-28 07:21

    The easiest way:

    -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
    {
       [manager stopUpdatingLocation];
        manager.delegate = nil;
    
       //...... do something
    
    }
    

    The manager can't find your didUpdateLocations method without the delegate reference :-D

    But don't forget to set it again before using startUpdatingLocation

    0 讨论(0)
提交回复
热议问题