Getting Altitude(height from sea level) of my location in iphone SDK

15.1k views Asked by At

I am trying to get Altitude of my location in my application. But every time I try altitude property of CLLocation object I am getting 0.00 as result.

I googled for my query and I got a similar question here and here. This link is saying that if I access CLLocation with wifi or cell tower it will be null. Even if I try to set desireAccuracy to best than even it don't make sure that app will use GPS. It is also saying that if I am indoor that I won't be able to access GPS.

I many cases it its not sure that app will use GPS only. I want some way for getting Altitude from wifi or cell tower too. For that I googled more and I got Google Earth API but I think this is for Microsoft .net technology only.

Now according to this situation I think for a solution I can create a web service in Microsoft Technology and pass my location there and I can get altitude as response but I don't want to do this.

Can anyone suggest me how to get my location's altitude from ios. Is there any way available or not? If yes than please navigate me in right direction.

Thanks in advance.

Edit1

I used CLLocationManager for updating location and when I get my location I need altitude.

Edit2

According to @fishinear's answer I tried following code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    manager = [[CLLocationManager alloc] init];
    [manager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
    [manager setDistanceFilter:kCLDistanceFilterNone];
    [manager setDelegate:self];
    [manager startUpdatingLocation];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    if ([newLocation verticalAccuracy] >= 0) {
        NSLog(@"Altitude = %lf",newLocation.altitude);
    }
    NSLog(@"Vertical Accuracy : %lf",newLocation.verticalAccuracy);
}

verticalAccuracy is always -1. It is not changing even after running the app to last 30 Mins. I am runnin ios 4.3.2 on my 3GS. I am indoor so I think it is not accessing GPS using this code even.

2

There are 2 answers

2
nvuono On BEST ANSWER

See Android - Get Altitude By Longitude and Latitude? there is an Android solution but it is easily adaptable to iOS.

The answer is to use the USGS Elevation Query Web Service

From the top answer there, basically you just send an HTML Web request to:

http://gisdata.usgs.gov/xmlwebservices2/elevation_service.asmx/getElevation?X_Value=" + 
             String.valueOf(longitude)   + 
             "&Y_Value=" + String.valueOf(latitude)             
             + "&Elevation_Units=METERS&Source_Layer=-1&Elevation_Only=true"; 

Then you can parse out the response text from between the <double>...</double> tags

Edit: It looks like you might be trying to get elevation outside the United States.

Google Maps API does provide an Elevation service and documentation at https://developers.google.com/maps/documentation/javascript/elevation

5
fishinear On

To answer your original question: GPS will always be used if you set desiredAccuracy to Best or BestForNavigation, and distanceFilter to kCLDistanceFilterNone when configuring the CLLocationManager. You will then also get altitude values in the CLLocation event.

However, it takes a short while before the GPS has "locked in" to enough satelites to be able to report the altitude. During that time it will report CLLocation events without altitude values. Therefore, you should ignore the first couple of events until the verticalAccuracy value is good enough for you. Also, check the timestamp, because sometimes the first CLLocation event is an old one.