简体   繁体   中英

CLLocationManager headingAvailable returns false

hi i'm in a situation here need help about heading data in ios, I'm making ai used CoreLocation framework in my application
my .h file

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

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

in .m file

@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];
}

the thing is i know that the simulator doesn't return the heading data, i'm using an ipod touch, but still [CLLocationManager headingAvailable] doesn't return true.this is kinda emergency please help any one

I am not quite sure, but the iPod touch does NOT include an GPS / compass module as far as i know it. It get its location information through a wifi database. Hence you won't get a heading because an iPod Touch is not capable to give it to you.

An iPod Touch has "only" a gyroscope and an accelerometer available. To read out information about these you have to use the Core Motion Framework

A CMMotionManager object is the gateway to the motion services provided by iOS. These services provide an application with accelerometer data, rotation-rate data, magnetometer data, and other device-motion data such as attitude. These types of data originate with a device's accelerometers and (on some models) its magnetometer and gyroscope.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM