2012-07-02 28 views
0

嗨,我在這裏的情況需要幫助瞭解iOS航向數據,我在做我過去CoreLocation框架在我的應用
我.h文件中CLLocationManager headingAvailable返回false

@interface ViewController : UIViewController<CLLocationManagerDelegate>{ 
    UIImageView *arrowImage; 
    UILabel *magneticHeadingLabel; 
    UILabel *trueHeadingLabel; 
    CLLocationManager *locationManager; 
} 

@property (nonatomic, retain) CLLocationManager *locationManager; 
@end 

在.m文件

@implementation ViewController 
@synthesize locationManager; 

-(void)locationManager:(CLLocationManager*)manager didUpdateHeading:(CLHeading*)newHeading { 

    NSLog(@"New magnetic heading: %f", newHeading.magneticHeading); 
    NSLog(@"New true heading: %f", newHeading.trueHeading); 
    NSLog(@"=>%@",[NSString stringWithFormat:@"%.1fmi",newHeading.headingAccuracy]); 
    NSString *headingstring = [[NSString alloc] initWithFormat:@"%f",newHeading.trueHeading]; 
    trueHeadingLabel.text = headingstring; 
    [headingstring release]; 

    NSString *magneticstring = [[NSString alloc]initWithFormat:@"%f",newHeading.magneticHeading]; 
    magneticHeadingLabel.text = magneticstring; 
    [magneticstring release]; 

} 

-(void)getHeadInfo{ 
    self.locationManager = [[CLLocationManager alloc] init]; 
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; 
    self.locationManager.delegate = self; 

    if ([CLLocationManager headingAvailable]) 
    {  
     NSLog(@"Heading Available...!"); 
     self.locationManager.headingFilter = kCLHeadingFilterNone; 
     [self.locationManager startUpdatingHeading]; 

    } 
    else { 
     NSLog(@"No Heading Available: "); 
     UIAlertView *noCompassAlert = [[UIAlertView alloc] initWithTitle:@"No Compass!" message:@"This device does not have the ability to measure magnetic fields." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [noCompassAlert show]; 
     [noCompassAlert release]; 
    } 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    arrowImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"right_arrow"]]; 
    arrowImage.frame = CGRectMake(95, 30, 128, 128); 
    [self.view addSubview:arrowImage]; 

    trueHeadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 300, 250, 30)]; 
    trueHeadingLabel.textAlignment = UITextAlignmentLeft; 
    trueHeadingLabel.textColor = [UIColor purpleColor]; 
    trueHeadingLabel.backgroundColor = [UIColor clearColor]; 
    trueHeadingLabel.font = [UIFont systemFontOfSize:13]; 
    trueHeadingLabel.text = [NSString stringWithFormat:@"trueHeadingLabel:"]; 
    [self.view addSubview:trueHeadingLabel]; 

    magneticHeadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 350, 250, 30)]; 
    magneticHeadingLabel.textAlignment = UITextAlignmentLeft; 
    magneticHeadingLabel.textColor = [UIColor purpleColor]; 
    magneticHeadingLabel.backgroundColor = [UIColor clearColor]; 
    magneticHeadingLabel.font = [UIFont systemFontOfSize:13]; 
    magneticHeadingLabel.text = [NSString stringWithFormat:@"magneticHeadingLabel:"]; 
    [self.view addSubview:magneticHeadingLabel]; 

    UIButton *start = [[UIButton alloc]initWithFrame:CGRectMake(140, 310, 50, 30)]; 
    [start setBackgroundColor:[UIColor redColor]]; 
    [start setTitle:@"start" forState:UIControlStateNormal]; 
    [start addTarget:self action:@selector(getHeadInfo) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:start]; 
    [start release]; 
} 

的事情是我知道的是,模擬器不返回的航向數據,我使用的是iPod touch的,但仍[CLLocationManager headingAvailable]不返回true.this是きnda緊急請幫助任何一個

回答

2

我不太確定,但iPod touch不包括GPS /指南針模塊,據我所知。它通過wifi數據庫獲取其位置信息。因此,您將無法獲得標題,因爲iPod Touch無法將其提供給您。

iPod Touch只有「陀螺儀」和加速計。爲了讀出這些信息,你必須使用核心運動框架

一個CMMotionManager對象是通往內部監督辦公室提供的運動服務 。這些服務爲加速度計數據,旋轉速率數據,磁力計數據以及其他姿態等設備運動數據提供應用程序 。這些類型的數據始於 與設備的加速度計和(在某些型號上)其磁力計 和陀螺儀。

+0

那麼它是如何運行的羅盤應用如.. http://itunes.apple.com/us/app/commander-compass-lite/id340268949?mt=8 – EarlySun

+0

編輯答案更多信息 – Pfitz