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