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

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

This my code......

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

    location_updated = [locations lastObj         


        
10条回答
  •  -上瘾入骨i
    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
    
    }
    

提交回复
热议问题