Question

I am pretty new to iOS coding. I need some help to fix an issue with CLLocation.

I use didUpdateLocations to retrieve location and speed information. I am able to get these details and display using NSLog.

However, I am unable to display it on UIview. How do I fix it?

Below is my code.

I use CoreLocationController to retrieve my location details.

@implementation CoreLocationController

@synthesize locMgr;
@synthesize location;

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

    if(self != nil) {
        self.locMgr = [[CLLocationManager alloc] init];
        self.locMgr.delegate = self;
    }

    return self;
}
- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations
{
    DriveTrapViewController *driveTrapViewController = [[DriveTrapViewController alloc]init];

    self.location = [locations lastObject];
    //NSLog(@"Speed, %f", self.location.speed);
    [driveTrapViewController locationUpdate:(CLLocation *)self.location];
}

I send the retrieved details back to DriveTrapViewController. I am able to display them using NSLog. But self.speedText.text is always NULL and nothing is showing on the screen.

@implementation DriveTrapViewController

@synthesize CLController;
@synthesize speedText = _speedText;
- (void)viewDidLoad {
    [super viewDidLoad];
    CLController = [[CoreLocationController alloc] init];

    CLController.delegate = self;
    [CLController.locMgr startUpdatingLocation];
}

- (void)locationUpdate:(CLLocation *)location 
{
    NSString *speed = [NSString stringWithFormat:@"SPEED: %f", location.speed];
    NSLog(@"Speed %@",speed);
    self.speedText.text = speed;
}

How do I fix this? Any help is appreciated. Thanks

Was it helpful?

Solution

Every time your locationManager:didUpdateLocations: method is called, it creates a new DriveTrapViewController, which is destroyed as soon as the method finishes. Maybe you should use an ivar for this instead. That way it wouldn't be created and destroyed every time.

Also, both objects seem to be making instances of the other. Instead of a Core Location problem, this is a general architecture-type problem.

I'd suggest that at this stage you should just have a CLLocationManager variable inside your DriveTrapController instead of trying to break it out into a controller of it's own.

OTHER TIPS

Try CLController.locMgr.desiredAccuracy = kCLLocationAccuracyBestForNavigation;. this should include some extra information about the users location, speed, bearing etc...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top