gpt4 book ai didi

objective-c - didUpdateToLocation 调用了多个

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:50:52 25 4
gpt4 key购买 nike

我有一个 iOS 应用程序使用用户的当前位置。我这样做:

-(void)startGeoloc{

NSLog(@"start geoloc");

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy=kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
}

#pragma mark - CLLocationManagerDelegate methods
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[locationManager stopUpdatingLocation];
AppDelegate *apDelegate =(AppDelegate *)[UIApplication sharedApplication].delegate;
apDelegate.modeGeoloc = YES;
[self callWebService:locationManager.location];

}

这个问题是我的方法 callWebService:locationManager.location 被调用了两次,我只想调用它一次。我该怎么做?谢谢你的回答

最佳答案

要确保 locationManager 只调用一次“didUpdate...”方法,请使用 BOOL 来引用是否已找到该位置。

  1. 创建 bool 变量:

    @property BOOL didFindLocation;
  2. 在 locationManager startUpdatingLocation 之前,将新的 BOOL 设置为 NO。这样您就可以随意调用新的位置更新。

    -(void) startFindingLocation {

    self.didFindLocation = NO; // like this

    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:self]
    [locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
    [locationManager startUpdatingLocation];
    }
  3. 在“didUpdateLocations”中检查它。

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

    if (!self.didFindLocation) {
    self.didFindLocation = YES;
    [locationManager stopUpdatingLocation];

    // do the rest of your stuff

    }
    }

如果可能,请不要在代码中的任何其他位置设置 didFindLocation 以避免混淆。

关于objective-c - didUpdateToLocation 调用了多个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9821716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com