CLLocationManager不接收更新

时间:2013-09-20 20:09:08

标签: ios objective-c geolocation

我是Objective-C世界的新手。我尝试在iOS 6.1上在模拟器上开发简单的位置应用程序。但是,当我尝试模拟位置时,它根本不起作用。我没有收到任何活动,但delegate对象已正确注册且PositionProviderstartUpdate

有什么问题?

我有以下代码:

PositionProvider

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

@interface PositionProvider : NSObject
{
    @public
    LocationListener<CLLocationManagerDelegate> *listener;
    @protected
    CLLocationManager *manager;
}

#pragma mark -
#pragma mark on/off

-(void) startUpdate;

-(void) stopUpdate;

@end

实施

    @implementation PositionProvider
{
@private
    CLLocation *lastLocation;
}

-(void) startUpdate{
    manager = [[CLLocationManager alloc] init];

    LocationListener *listener2 = [[LocationListener alloc]init];

    manager.delegate = listener2;
    manager.desiredAccuracy = kCLLocationAccuracyBest;

    if(manager == nil)
        NSLog(@"Manager is NULL");

    if(listener == nil || manager.delegate == nil)
        NSLog(@"Listener is NULL");

    NSLog(@"startUpdate");

    if ([CLLocationManager locationServicesEnabled]) {
        NSLog(@"Location Services Enabled");
        switch ([CLLocationManager authorizationStatus]) {
            case kCLAuthorizationStatusAuthorized:
                NSLog(@"We have access to location services");
                break;
            case kCLAuthorizationStatusDenied:
                NSLog(@"Location services denied by user");
                break;
            case kCLAuthorizationStatusRestricted:
                NSLog(@"Parental controls restrict location services");
                break;
            case kCLAuthorizationStatusNotDetermined:
                NSLog(@"Unable to determine, possibly not available");
                break;
            default:
                break;
        }
    }
    else {
        NSLog(@"Location Services Are Disabled");
    }

    [manager startUpdatingHeading];
    [manager startUpdatingLocation];

}

-(void) stopUpdate{



}

@end

LocationListener的

@interface LocationListener : NSObject<CLLocationManagerDelegate>

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

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



    - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status;

@end

实施

@implementation LocationListener 

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

//    newLocation.coordinate;

    NSLog(@"Logging Location");
}

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

    NSLog(@"Logging Location");
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus) status{


    NSLog(@"Status");
}

@end

1 个答案:

答案 0 :(得分:0)

你试过吗

[manager setDelegate:listener2];

或者,您可能遇到内存管理问题。尝试使用调试器断点添加dealloc方法,看看它是否在您预期之前被调用:

- (void)dealloc {
    NSLog(@"Dealloc called");

    self.locationManager = nil;
}