CLLocationManager仅适用于第一次

时间:2014-05-16 17:17:31

标签: ios objective-c xcode cllocationmanager

我在CLLocationManager类附近编写了一个包装器。使用UIViewControllers类有不同的LocationManager。这是一个单身人士,我在使用后将其设置为零。但是,它仅适用于使用它的第一个类。怎么杀了?我已经打电话给stopUpdatingLocation

我试过了this already

this post suggests我不应该使用单身人士,但如果我这样做,那就根本不起作用。

MyViewController.m

LocationManager * locationManager;

@implementation MyViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    locationManager = [LocationManager sharedManager];
    [locationManager start];
    locationManager.delegate = self;
}

#pragma mark - Delegate functions

-(void)isLocationManagerDelegateTriggered:(CLLocation *) location {
    // works only first time, but gets triggered every time    
    [locationManager stop];
    locationManager.delegate = nil;
    NSLog(@"latitude = %f longitude = %f",location.coordinate.latitude, location.coordinate.longitude);
}

LocationManager.m

#import <CoreLocation/CoreLocation.h>

@implementation LocationManager

CLLocationManager *clLocationManager;

@synthesize delegate;

/* Singleton */
static LocationManager *sharedSingleton;

/* Singleton */
+ (id)sharedManager
{
    static LocationManager *sharedMyManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedMyManager = [[self alloc] init];
        clLocationManager = [[CLLocationManager alloc] init];
    });
    return sharedMyManager;
}

#pragma mark - Public functions

- (void)start {

    [clLocationManager setDelegate:(id<CLLocationManagerDelegate>)self]; //If I put this         [clLocationManager setDesiredAccuracy:kCLLocationAccuracyBest]; // and this in initialize, it doesn't work anymore.

    [clLocationManager startUpdatingLocation];
}

- (void)stop {
    [clLocationManager stopUpdatingLocation];
}

#pragma mark - CLLocationManagerDelegate

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

        if (currentLocation != nil) {
            [delegate isLocationManagerDelegateTriggered:currentLocation];
        }
}

@end

LocationManager.h

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>

@class LocationManager;

@protocol LocationManagerDelegate

@required
-(void)isLocationManagerDelegateTriggered:(CLLocation *) location;
@end

@interface LocationManager : NSObject <CLLocationManagerDelegate>

+ (id)sharedManager;
-(void)start;
- (void)stop;
- (double) getDistanceToLocationInKM:(CLLocation *) location currentLocation:(CLLocation *)currentlocation;

@property (nonatomic, assign) id  delegate;

@end

委托只被解雇一次:

2014-05-17 20:04:03.110 Wobbly [2064:60b] latitude = 50.865281经度= 5.732814

1 个答案:

答案 0 :(得分:2)

您正在使用以下方式分配对象:

locationManager = [[LocationManager alloc]init];

它不会创建单例实例。

而不是你需要使用:

我在CLLocationManager类周围写了一个包装器。使用LocationManager类有不同的UIViewControllers。这是一个单身人士,我在使用后将其设置为零。但是,它仅适用于使用它的第一个类。怎么杀了?我已经在调用stopUpdatingLocation。

我已经尝试过了

这篇文章暗示我不应该使用单身,但如果我这样做,它根本不起作用。

<强> MyViewController.m

LocationManager * locationManager;

@implementation MyViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    locationManager = [LocationManager sharedManager];
    [locationManager start];
    locationManager.delegate = self;
}

#pragma mark - Delegate functions

-(void)isLocationManagerDelegateTriggered:(CLLocation *) location
{
    // works only first time, but gets triggered every time    
    [locationManager stop];
    locationManager.delegate = nil;
}

<强> LocationManager.m

#import <CoreLocation/CoreLocation.h>

@implementation LocationManager

CLLocationManager *clLocationManager;

@synthesize delegate;

/* Singleton */
+ (id)sharedManager
{
    static LocationManager *sharedMyManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedMyManager = [[self alloc] init];
        clLocationManager = [[CLLocationManager alloc] init];
    });
    return sharedMyManager;
}

#pragma mark - Public functions

- (void)start
{

    [clLocationManager setDelegate:(id<CLLocationManagerDelegate>)self]; //If I put this         [clLocationManager setDesiredAccuracy:kCLLocationAccuracyBest]; // and this in initialize, it doesn't work anymore.

    [clLocationManager startUpdatingLocation];
}

- (void)stop
{
    [clLocationManager stopUpdatingLocation];
}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    if(firsttime)
    {
        firsttime = false;
        CLLocation *currentLocation = newLocation;

        if (currentLocation != nil)
        {
            [delegate isLocationManagerDelegateTriggered:currentLocation];
        }
    }
}

@end

<强> LocationManager.h

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>

@class LocationManager;

@protocol LocationManagerDelegate

@required
-(void)isLocationManagerDelegateTriggered:(CLLocation *) location;
@end

@interface LocationManager : NSObject <CLLocationManagerDelegate>

+ (id)sharedManager
- (void)start;
- (void)stop;
- (double) getDistanceToLocationInKM:(CLLocation *) location currentLocation:(CLLocation *)currentlocation;

@property (nonatomic, assign) id  delegate;

@end