检查CLLocationManager的实例是否为isUpdatingLocation

问题描述 投票:4回答:4

我需要知道CLLocationManager的一个实例(在这种情况下称为gps)是否正在更新位置调用正确的方法

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

在我的情况下,完整的方法是:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    //NSLog(@"didUpdateToLocation: %@", newLocation);
    currentLocation = newLocation;

    if (currentLocation != nil) {
        longitude.text = [NSString stringWithFormat:@"%.3f", currentLocation.coordinate.longitude];
        latitude.text = [NSString stringWithFormat:@"%.3f", currentLocation.coordinate.latitude];
    }

    address.text = NULL;
    [activityIndicator setHidden:NO];
    [activityIndicator startAnimating];
    NSLog(@"Resolving the Address");
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        //NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
        if (error == nil && [placemarks count] > 0) {
            placemark = [placemarks lastObject];
            [address sizeToFit];
            address.text = [NSString stringWithFormat:@"%@, %@\n%@ %@\n%@",
                            [self sanitizedDescription:placemark.thoroughfare],
                            [self sanitizedDescription:placemark.subThoroughfare],
                            [self sanitizedDescription:placemark.postalCode],
                            [self sanitizedDescription:placemark.locality],
                            [self sanitizedDescription:placemark.country]];


            if (address.text != NULL)
            {
                gpsTimer = [NSTimer scheduledTimerWithTimeInterval:10
                                                            target:gps
                                                          selector:@selector(startUpdatingLocation)
                                                          userInfo:nil
                                                           repeats:NO];
                [address sizeToFit];
                [gps stopUpdatingLocation];
            }
        } else {
            NSLog(@"%@", error.debugDescription);
        }
    } ];

}

我能怎么做?

提前致谢 :)

ios objective-c cllocationmanager
4个回答
8
投票

子类CLLocationManager并添加一个BOOL,当你的委托方法被调用时设置为yes ...

您还可以简单地告诉您的位置管理器在委托方法中停止


2
投票

下面是一个简单的示例代码,用于说明如何处理此问题。

import Foundation
import CoreLocation

public class LocationManager : NSObject, CLLocationManagerDelegate{  

    var locationManager: CLLocationManager?
    public var isLocationUpdate: Bool = false


     override public init() {
        super.init()

        self.locationManager = CLLocationManager()
        self.locationManager!.delegate = self

        if #available(iOS 9.0, *) {            
            self.locationManager!.requestWhenInUseAuthorization()          
        }
        self.locationManager!.startUpdatingLocation()
        self.isLocationUpdate = false
    }


public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
   self.isLocationUpdate = true
  print(locations)

}


public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {

        //Retrieving the error code.
      if let code = (error as NSError).code{
        isLocationUpdate = false
      }
}

0
投票

Apple在iOS 6中弃用了一个主要的位置管理器委托方法,因此如果您支持旧版本的iOS以及当前版本,则需要支持两种委托方法:

iOS 2 - iOS 5:

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

iOS 6及更高版本:

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

0
投票

你真的没办法检查“这个位置管理员的”我的代表是否活跃?“。

您需要做的是跟踪自己是否已完成位置更新。

我不确定这是否是你需要的,但这就是我的所作所为:

1-当我开始请求更新时,我会像你一样,即通过requestlocationupdates和一个委托。

2-如果我没有得到我需要的更新,我想在一段时间后超时,以便我的应用程序不会永远等待一个位置,在这种情况下我使用一个超时的选择器:

[self performSelector:@selector(fetchLocationTimeout) withObject:nil afterDelay:LOCATION_LISTEN_TIME];

并在选择器中:

(void)fetchLocationTimeout{
    if(!finished){
        [self stopUpdatingLocation];    
        [delegate locationFinished:bestEffortAtLocation];//see more on this below
    }
}

3-在我的didupdate-delegate中我都存储了我已经获得的“最佳”位置,如果我确定新位置“足够好”,我完成了我的位置获取过程:

(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{

    if(newLocation is the best i have){

        self.bestEffortAtLocation = newLocation;

    }
// test the measurement to see if it is more accurate than the previous measurement

    if (bestEffortAtLocation == nil || newLocation is the best i have) {
// store the location as the "best effort"
        if (bestEffortAtLocation is good enough and i want to use it) {        
            [self stopUpdatingLocation];            
        // we can also cancel our previous performSelector:withObject:afterDelay: - it's no longer necessary
        SEL selector = NSSelectorFromString(@"fetchLocationTimeout");
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:selector object:nil];
            [delegate locationFinished:bestEffortAtLocation];
        }
    }
}

最后,这是我在上面调用的方法,既可以在didupdate-delegate中,也可以从选择器超时中减少竞争条件的风险:

(void)stopUpdatingLocation{
    finished = true;
    NSLog(@"stopUpdatingLocation! manager: %@", [locationManager description]);
    [locationManager stopUpdatingLocation];
    locationManager.delegate = nil;
}

最后的注释:当我完成时我正在调用的“委托”是我自己的界面,描述了一个知道如何处理位置获取过程结果的类。正如您所看到的,它只有一种方法可以实现“locationFinished”。

希望这有帮助,即使我的代码粘贴技能不完全是忍者。

© www.soinside.com 2019 - 2024. All rights reserved.