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

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

This my code......

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

    location_updated = [locations lastObj         


        
10条回答
  •  感动是毒
    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
    }
    

提交回复
热议问题