开发者

Location and heading not working

I downloaded the source code of this tutorial and played a bit with it. All working fine.

Now I wanted to create my own project to implement location and heading. But after writing all, it does not work. I don't even see a GPS indication on the top.

When I run the other app, its al fine. Xcode does not give any errors or warnings.

my code: (it did not change anything in the AppDelegate).

LocationAndHeadingTestViewController.h

#import <UIKit/UIKit.h>
#import "LocationController.h"

@interface LocationAndHeadingTestViewController : UIViewController <LocationControllerDelegate>
{
    LocationController *_locationController;

    IBOutlet UILabel *_errorLabel;

    //Location
    IBOutlet UILabel *_locationTimeLabel;
    IBOutlet UILabel *_latitudeLabel;
    IBOutlet UILabel *_longitudeLabel;
    IBOutlet UILabel *_altitudeLabel;

    //Heading
    IBOutlet UILabel *_headingTimeLabel;
    IBOutlet UILabel *_trueHeadingLabel;
    IBOutlet UILabel *_magneticHeadingLabel;
    IBOutlet UILabel *_headingAccuracyLabel;

    IBOutlet UIImageView *_compass;
}

@property (nonatomic, retain) LocationController *locationController;

@end

LocationAndHeadingTestViewController.m

#import "LocationAndHeadingTestViewController.h"

@implementation LocationAndHeadingTestViewController

@synthesize locationController = _locationController;

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    _locationController = [LocationController new];
    _locationController.delegate = self;
    [_locationController.locationManager startUpdatingLocation];
    [_locationController.locationManager startUpdatingHeading];
}


- (void)locationUpdate:(CLLocation *)location 
{
    [_locationTimeLabel setText:[NSString stringWithFormat:@"%@", location.timestamp]];
    [_latitudeLabel setText:[NSString stringWithFormat:@"%f", location.coordinate.latitude]];
    [_longitudeLabel setText:[NSString stringWithFormat:@"%f", location.coordinate.longitude]];
    [_altitudeLabel setText:[NSString stringWithFormat:@"%f", [location altitude]]];
}

- (void)headingUpdate:(CLHeading *)heading
{
    [_headingTimeLabel setText:[NSString stringWithFormat:@"%@", heading.timestamp]];
    [_trueHeadingLabel setText:[NSString stringWithFormat:@"%f", heading.trueHeading]];
    [_magneticHeadingLabel setText:[NSString stringWithFormat:@"%f", heading.magneticHeading]];
    [_headingAccuracyLabel setText:[NSString stringWithFormat:@"%f", heading.headingAccuracy]];
}

- (v开发者_运维技巧oid)locationError:(NSError *)error 
{
    _errorLabel.text = [error description];
}

- (void)didReceiveMemoryWarning 
{
    [super didReceiveMemoryWarning];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)viewDidUnload 
{
    [super viewDidUnload];
}

- (void)dealloc 
{
    [_locationController release];
    [super dealloc];
}

@end

LocationController.h

#import <CoreLocation/CoreLocation.h>

@protocol LocationControllerDelegate
@required

- (void)locationUpdate:(CLLocation *)location;
- (void)headingUpdate:(CLHeading *)heading;
- (void)locationError:(NSError *)error;

@end

@interface LocationController : NSObject <CLLocationManagerDelegate>
{
    CLLocationManager *_locationManager;
    id _delegate;
}

@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, assign) id delegate;

@end

LocationController.m

#import <CoreLocation/CoreLocation.h>
#import "LocationController.h"

@implementation LocationController

@synthesize locationManager = _locationManager, delegate = _delegate;

- (id)init
{
    if(self = [super init]) 
    {
        _locationManager = [[CLLocationManager new] autorelease];
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        _locationManager.delegate = self;
    }

    return self;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{
    if([self.delegate conformsToProtocol:@protocol(LocationControllerDelegate)]) 
    {
        [self.delegate locationUpdate:newLocation];
    }
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{
    if([self.delegate conformsToProtocol:@protocol(LocationControllerDelegate)]) 
    {
        [_delegate locationError:error];
    }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
    if([self.delegate conformsToProtocol:@protocol(LocationControllerDelegate)]) 
    {
        [_delegate headingUpdate:newHeading];
    }
}

- (void)dealloc 
{
    [_locationManager release];
    [super dealloc];
}

@end

It is fairly simple but I think I really overlook something.

Ps. I will change the id delegate to id <..> delegate


You shouldn't be autoreleasing the CLLocationManager object:

_locationManager = [[CLLocationManager new] autorelease];

That's a "retain" property. You need to remove the autorelease, you are going to release it in the dealloc. You can only release it once.

If you had assigned it using the setter, then the setter would have retained it for you. Like this would be OK:

self.locationManager = [[CLLocationManager new] autorelease];


Is your app enabled for location service in the iPhone settings?

Location service (global access) can be on but your specific app in the location service settings could also be denied location access at the same time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜