5

我一次又一次听说总有比单例更好的模式,但我不明白我的应用程序如何在不等待 GPS 返回数据的情况下访问设备位置(我假设位置系统仅在明确要求时运行,如有错误请纠正我)。

那么从多个(不相关的)控制器访问 CLLocation 数据是否有更好的模式?或者即使我没有通过 CLLocationManager 访问它,我是否可以期望设备位置在后台更新?

4

1 回答 1

1

声明一个类。像下面这样。

我的位置.h

@protocol MyCLControllerDelegate <NSObject>
- (void)locationUpdate:(CLLocation *)location; 
- (void)locationError:(NSError *)error;
@end

@interface MyLocation : NSObject <CLLocationManagerDelegate> {

    CLLocationManager *locationManager;
    id delegate;
}
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) id <MyCLControllerDelegate> delegate;

我的位置.m

#import "MyLocation.h"
@implementation MyLocation

@synthesize locationManager;
@synthesize delegate;

- (id) init {
    self = [super init];
    if (self != nil) {

        if([CLLocationManager locationServicesEnabled]) {
            if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted )
            {
                [self showAlertWithTitle:@"Warning" andWithMessage:@"Determining your current location cannot be performed at this time because location services are enabled but restricted"   forTargetView:self];

NSlog(@"Determining your current location cannot be performed at this time because location services are enabled but restricted");

            }
            else
            {
                self.locationManager = [[CLLocationManager alloc] init];
                self.locationManager.delegate = self; // send loc updates to myself
                [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
                [self.locationManager setDistanceFilter:kThresholdDistance];
                [self.locationManager startUpdatingLocation];

                NSLog(@"Location sharing set ON!");

            }


        } else {
            [MobileYakHelper showAlertWithTitle:@"Error" andWithMessage:@"Determining your current location cannot be performed at this time because location services are not enabled." forTargetView:self];

            NSLog(@"Location sharing set OFF!");

        }

    }
    return self;
}

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

    NSDictionary *dictValue = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithDouble:newLocation.coordinate.latitude], @"latitude",
                               [NSNumber numberWithDouble:newLocation.coordinate.longitude], @"longitude",
                               nil];

    [[NSUserDefaults standardUserDefaults] setValue:dictValue forKey:@"MY_LOCATION"];

    CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation];
    if (meters >= kThresholdDistance ) {
        [self.delegate locationUpdate:newLocation];

    }
}


- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error
{


    [self.delegate locationError:error];
}



@end

要在它的 .h 文件中的少数控制器 .Adapt 委托中使用它,并使用如下:

- (void) initializeLocations
{
    MyLocation _myLocation = [[MyLocation alloc] init];
    _myLocation.delegate = self;
    _myLocation.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    CLLocation * currentLocation =  _myLocation.locationManager.location;
        // Updating user's current location to server
    [self sendUserCurrentCoordinate:currentLocation.coordinate];

        // start updating current location
    _myLocation.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [_myLocation.locationManager startUpdatingLocation];

    [_myLocation.locationManager startUpdatingLocation];
}

- (void)locationUpdate:(CLLocation *)location {


          NSLog(@"location %@", location); 
}

- (void)locationError:(NSError *)error {

    NSLog(@"locationdescription %@", [error description]);

}
于 2013-09-23T06:59:55.050 回答