Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Altitude is always zero on iPhone simulator

I'm trying to develop an Altimeter on Xcode and Simulator but it always return 0 for the height above the sea. I don't understand why, I've tried with a lot of places on Simulator.

My code is this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _mapView.delegate = self;
    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    _locationManager.pausesLocationUpdatesAutomatically = YES;
    _locationManager.delegate = self;

    firstLocation = YES;
   checkUserLocation = NO;
}

- (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation *location = [locations lastObject];
lastLocation = location.coordinate;
_latitudeLabel.text = [NSString stringWithFormat:@"Current latitude: %f", location.coordinate.latitude];
_longitudeLabel.text = [NSString stringWithFormat:@"Current longitude: %f", location.coordinate.longitude];
_altitudeLabel.text = [NSString stringWithFormat:@"Current altitude: %f m", location.altitude];
}

Where is my error?

like image 799
emattiazzi Avatar asked Feb 07 '14 15:02

emattiazzi


1 Answers

There's nothing wrong with your code, its because you are using the simulator.

Determining altitude requires a device with GPS capabilities, and you also need to be using GPS on that device in order to get it (wifi only location would not report altitude correctly even on GPS-enabled devices). The iOS simulator does not have those capabilities so altitude will not be accurate there. You will need to use a real device with GPS to get altitude measurements.

If you want to simulate a CLLocation with altitude you can create your CLLocation object and pass it an altitude yourself:

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate
    altitude:(CLLocationDistance)altitude
    horizontalAccuracy:(CLLocationAccuracy)hAccuracy
    verticalAccuracy:(CLLocationAccuracy)vAccuracy
    timestamp:(NSDate *)timestamp

Altitude is a readonly property so you will need to create a new CLLocation object yourself instead of changing it manually when you receive a CLLocation object in your delegate callback.

like image 130
foggzilla Avatar answered Oct 11 '22 20:10

foggzilla