如何在ios中多次停止调用didUpdateLocations()的方法

时间:2014-03-10 05:43:16

标签: ios iphone cllocationmanager cllocation

这是我的代码......

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

    location_updated = [locations lastObject];
    NSLog(@"updated coordinate are %@",location_updated);
    latitude1 = location_updated.coordinate.latitude;
    longitude1 = location_updated.coordinate.longitude;

    self.lblLat.text = [NSString stringWithFormat:@"%f",latitude1];
    self.lblLon.text = [NSString stringWithFormat:@"%f",longitude1];

    NSString *str = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false",latitude1,longitude1];
    url = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    connection = [NSURLConnection connectionWithRequest:request delegate:self];
    if (connection)
    {
        webData1 = [[NSMutableData alloc]init];
    }
        GMSMarker *marker = [[GMSMarker alloc] init];
        marker.position = CLLocationCoordinate2DMake(latitude1,longitude1);
        marker.title = formattedAddress;
        marker.icon = [UIImage imageNamed:@"m2.png"];
        marker.map = mapView_;
        marker.draggable = YES;
 }

这种方法多次调用,我不想......

10 个答案:

答案 0 :(得分:37)

在分配LocationManager对象时,您可以设置distanceFilter的{​​{1}}属性。距离过滤器属性是LocationManager值,可以将其设置为通知位置管理器有关以米为单位移动的距离。您可以按如下方式设置距离过滤器:

CLLocationDistance

答案 1 :(得分:21)

在那里添加一些限制。对于位置和准确度之间的时间跨度

-(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

}

答案 2 :(得分:21)

最简单的方法:

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

   //...... do something

}

如果没有委托引用,管理员找不到您的 didUpdateLocations 方法:-D

但是在使用 startUpdatingLocation

之前不要忘记再次设置它

答案 3 :(得分:10)

我有类似的情况。您可以使用dispatch_once:

static dispatch_once_t predicate;

- (void)update
{
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined &&
        [_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [_locationManager requestWhenInUseAuthorization];
    }

    _locationManager.delegate = self;
    _locationManager.distanceFilter = kCLDistanceFilterNone;
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    predicate = 0;
    [_locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    [manager stopUpdatingLocation];
    manager = nil;

    dispatch_once(&predicate, ^{
        //your code here
    });
}

答案 4 :(得分:1)

您可以使用静态变量存储最新的位置时间戳,然后将其与最新的位置时间戳进行比较,如下所示:

- (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
}

答案 5 :(得分:0)

如果您想要停止更新位置管理器

,请编写此方法
[locationManager stopUpdatingLocation];

答案 6 :(得分:0)

对于时间限制,我不理解接受的答案中的代码,发布了不同的方法。正如Rob指出的那样#34;当你第一次启动位置服务时,你可能会多次看到它被叫出来#34;下面的代码作用于第一个位置,并在前120秒忽略更新的位置。这是解决原始问题的一种方法&#34;如何多次停止方法调用didUpdateLocations&#34;。

在.h文件中

@property(strong,nonatomic) CLLocation* firstLocation;
在.m文件中

// is this the first location?
    CLLocation* newLocation = locations.lastObject;
    if (self.firstLocation) {
        // app already has a location
        NSTimeInterval locationAge = [newLocation.timestamp timeIntervalSinceDate:self.firstLocation.timestamp];
        NSLog(@"locationAge: %f",locationAge);
        if (locationAge < 120.0) {  // 120 is in seconds or milliseconds?
            return;
        }
    } else {
        self.firstLocation = newLocation;
    }

    // do something with location

答案 7 :(得分:0)

你可以设置一个标志(Bool)。当您实例化locationsManager set flag = true时,当locationManager:didUpdateLocations在代码块内返回时 你想只运行一次set flag = false。这样它只会运行一次。

 if flag == true {
     flag = false
    ...some code probably network call you only want to run the once 
    }

位置管理器将被多次调用,但是您想要只执行一次代码,我认为这是您要实现的目标?

答案 8 :(得分:0)

locationManager.startUpdatingLocation()连续获取位置并且didUpdateLocations方法多次调用, 只需在调用locationManager.startUpdatingLocation()之前设置locationManager.distanceFilter值的值。

当我设置200米(你可以根据你的要求改变)工作正常

    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.distanceFilter = 200
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()

答案 9 :(得分:0)

你可以写: [经理stopUpdatingLocation]; 经理=零; 在didupdatelocation委托