2

私はこれのいくつかの例を見ましたが、それを実装する方法が正確にわかりません(iPhone Compass GPS Directionなど)。(void)startupでcalcBearingWithLatitudeを呼び出す必要がありますか?

私がやろうとしているのは、コンパスにメモリからポイントを引き出し、そのポイントをいわば「北」として使用して、距離などを必要としないことです。

今のところ私の実装ファイルは次のようになります

@implementation TrackerViewController

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

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

if ([CLLocationManager headingAvailable]) {

} else {
    [[[[UIAlertView alloc] initWithTitle:@"Error" 
                                 message:@"Device doesn't support heading updates" 
                                delegate:nil 
                       cancelButtonTitle:@"OK" 
                       otherButtonTitles:nil] autorelease] show];   
}
}

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


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

//  NSLog(@"Error!");

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

 - (void)viewDidLoad {
 locationManager=[[CLLocationManager alloc] init];
 locationManager.desiredAccuracy = kCLLocationAccuracyBest;
 locationManager.delegate=self;
 //Start the compass updates.
 [locationManager startUpdatingHeading];
 [super viewDidLoad];
 }

- (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)dealloc {
self.locationManager = nil;
self.compass = nil;
self.headingLabel = nil;
[super dealloc];
}

@end
4

1 に答える 1

2

座標 A から座標 B への方位を見つけ、その方位を座標 A から座標 B へのコンパス方位のオフセットとして使用します。

座標への方位を見つけるための 2 つのリンクを次に示します。

2 つの CLLocationCoordinate2D 間の方位を計算する

http://shawnsbits.com/blog/2011/07/07/calculating-heading-with-corelocation/

コンパスの方位を常に座標 B を指すように移動に合わせて更新する場合は、新しい方位または位置を取得するたびに再計算する必要があります。

于 2012-05-03T22:49:08.937 に答える