2011-09-30 100 views
0

我下載了this教程的源代碼,並與它玩了一下。 所有工作正常。位置和標題不起作用

現在我想創建自己的項目來實現位置和標題。但寫完後,它不起作用。我甚至沒有看到頂部有GPS指示。

當我運行其他應用程序,它很好。 Xcode不會給出任何錯誤或警告。

我的代碼: (它沒有改變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]]; 
} 

- (void)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 

這很簡單,但我認爲我真的忽略了一些東西。

Ps。我的ID代表變更爲ID < ..>委託

回答

1

你不應該自動釋放CLLocationManager對象:

_locationManager = [[CLLocationManager new] autorelease]; 

這是一個「保留」屬性。你需要刪除autorelease,你將在dealloc中釋放它。你只能釋放一次。

如果你已經使用setter分配它,那麼setter將會爲你保留它。像這樣可以:

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

很好的解釋和它的工作。真的感謝。 – Justin

0

您的應用程序是否啓用了iPhone設置中的位置服務?

位置服務(全局訪問)可以打開,但位置服務設置中的特定應用程序也可能會同時拒絕位置訪問。