2011-02-24 16 views
0

안녕하세요 여러분, 미리 지정된 위치의 방향을 가리 키도록 나침반을 사용하는 앱을 만들려고하고 있습니다.하지만 지금은 기본 나침반 만드는 법을 이해하려고합니다. 나는 가이드를 따랐다. 그러나 나는 항상 헤더로서 "-1"을 얻었고, 한번만 작동하도록했다. 그리고 나는 코드를 변경하지 않았다. 어떤 아이디어라도 도움이 될 것입니다. CLLocation 헤더는 항상 -1입니다.

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

@interface TrackerViewController : UIViewController <CLLocationManagerDelegate> { 

} 

@property (nonatomic, retain) CLLocationManager *locationManager; 
@property (nonatomic, retain) IBOutlet UISwitch *toggle; 
@property (nonatomic, retain) IBOutlet UIImageView *compass; 
@property (nonatomic, retain) IBOutlet UILabel *headingLabel; 

-(IBAction)toggleSwitch; 

@end 

및 구현 파일

...

#import "TrackerViewController.h" 


@implementation TrackerViewController 

@synthesize locationManager; 
@synthesize toggle; 
@synthesize compass; 
@synthesize headingLabel; 

- (IBAction)toggleSwitch { 
if(self.toggle.isOn) { 
    [self.locationManager startUpdatingHeading]; 
} else { 
    [self.locationManager stopUpdatingHeading]; 
} 
} 

- (void)viewWillAppear:(BOOL)animated { 
[super viewWillAppear:animated]; 

if ([CLLocationManager headingAvailable]) { 
    self.locationManager = [[[CLLocationManager alloc] init] autorelease]; 
    self.locationManager.delegate = self; 
} else { 
    [[[[UIAlertView alloc] initWithTitle:@"Aw snap!" 
           message:@"Device doesn't support heading updates" 
           delegate:nil 
         cancelButtonTitle:@"OK" 
         otherButtonTitles:nil] autorelease] show]; 
} 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading 
*)newHeading { 
float heading = [newHeading trueHeading] * M_PI/180.0; 
self.compass.transform = CGAffineTransformMakeRotation(-heading); 
self.headingLabel.text = [NSString stringWithFormat:@"%d°", (int)[newHeading trueHeading]]; 
NSLog(@"%d", (int)[newHeading trueHeading]); 
} 


- (void)locationManager:(CLLocationManager *)manager 
    didFailWithError:(NSError *)error { 

// NSLog(@"Error!"); 

if (error.code == kCLErrorDenied) { 
    [[[[UIAlertView alloc] initWithTitle:@"Aw snap!" 
           message:@"User didn't allow heading updates" 
           delegate:nil 
         cancelButtonTitle:@"OK" 
         otherButtonTitles:nil] autorelease] show]; 
    self.toggle.on = NO; 
    [self.locationManager stopUpdatingHeading]; 
} 
} 


- (void)didReceiveMemoryWarning { 
// Releases the view if it doesn't have a superview. 
[super didReceiveMemoryWarning]; 

// Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
// Release any retained subviews of the main view. 
// e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
self.locationManager = nil; 
self.compass = nil; 
self.toggle = nil; 
self.headingLabel = nil; 
[super dealloc]; 
} 

@end 
+0

"trueHeading"(didUpdateHeading 내에서)의 모든 참조가 "magneticHeading"으로 바뀔 때마다 매번 작동 함을 알았습니다. 자기보다 정확하고 정확하지 않습니다. –

답변

0

magneticHeading 이 속성의 값은 지리적 북극 상이한 자기 북극에 대해 방위를 나타낸다. 값 0은 장치가 자기 북쪽을 향하고 90은 동쪽을 가리키고 180은 남쪽을 가리킨다는 것을 의미합니다. 이 속성의 값은 항상 유효해야합니다.