iOS CoreLocation Altitude
Asked Answered
H

4

10

Prior to iOS 4.0, CoreLocation was reporting altitude correctly, now it always reports as 0 ft.

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    NSString *tLatitude  = [NSString stringWithFormat:@"%3.5f", newLocation.coordinate.latitude]; 
    NSString *tLongitude = [NSString stringWithFormat:@"%3.5f", newLocation.coordinate.longitude];
    /*  the following returns 0 */
    NSString *tAltitude  = [NSString stringWithFormat:@"%i",    newLocation.altitude];

    /* theres more code, but it's not relevant, 
                      and this worked prior to iOS 4.0*/
    [manager stopUpdatingLocation];
}

Not working on Device nor Simulator, does anyone else experience this issue?

Hooknosed answered 2/2, 2011 at 3:20 Comment(1)
Are you testing this on a device, or on the Simulator?Apteryx
P
12

If you're using startMonitoringSignificantLocationChanges, which was a feature new to iOS 4.0, then you will not get altitude updates. This low-power mode only uses cell towers to figure out a user's location, and this method not report altitude.

More generally, the iPhone has three ways of figuring out your location -- cell towers, wi-fi, and GPS. You will only get altitude when the GPS is being used. So even if you set your desiredAccuracy setting to be really precise to force the device to use GPS, if a user is indoors the iPhone probably won't be able to get a GPS signal and will fallback to cell or wi-fi. In that case, you won't get an altitude. Also consider users who are on an iPod Touch -- it only has the ability to get a location via wi-fi, and thus also won't report an altitude.

Python answered 27/1, 2012 at 3:30 Comment(0)
A
2

Two things you could try. First of all, try setting the location manager's desiredAccuracy to kCLLocationAccuracyBest.

If that doesn't work, try removing [manager stopUpdatingLocation];, and put a NSLog in the didUpdateToLocation with the altitude. Sometimes it needs to narrow down for a bit before it displays the altitude.

Apteryx answered 2/2, 2011 at 4:17 Comment(1)
kCLLocationAccuracyBest has always been set, and removing stopUpdatingLocation has no effect.Hooknosed
D
1

From the Core Location Data Types Reference:

The CLLocationDistance data type (the data type for altitude) is a double. The formatter you are using for your stringWithFormat is an integer. You need to cast your altitude to an integer, first, or use a double (%f) formatter.

Deonnadeonne answered 2/2, 2011 at 3:31 Comment(3)
I am converting altitude to an integer. As I stated, this worked perfectly (ie. no code changed) prior to iOS 4.0.Hooknosed
Also, the way I have it /gave/ me perfect altitude, used to show for example 5280 ft.Hooknosed
@Hooknosed have you tried the double formatter as suggested to see if that makes a difference?Rerun
H
0

Core Location does not always report altitude immediately. I usually check for a zero altitude in my "success" delegate method. If the altitude is still 0, I keep checking, otherwise, I turn of CoreLocation.

Haywoodhayyim answered 31/5, 2011 at 13:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.