Skip to content

GeolocationFailed: Missing altitude above WGS84 when location has no altitude data (Android) #323

@dosier

Description

@dosier

Problem

When using MobileGeolocator().current() on Android, the call fails with the error:

GeolocationFailed: Missing altitude above WGS84

This happens when the device returns a location without altitude data (common with network-based positioning via WiFi/cell towers rather than GPS).

Root Cause

In Mapper.kt, the addMslAltitudeToLocation() function is called unconditionally:

internal suspend fun Location.toModel(context: Context): dev.jordond.compass.Location {
    return withContext(Dispatchers.IO) {
        addMslAltitudeToLocation(context, this@toModel)  // <-- Called even when hasAltitude() is false
        // ...
    }
}

The AndroidX AltitudeConverterCompat.addMslAltitudeToLocation() requires the location to have altitude data. When hasAltitude() returns false, this function throws an exception.

Expected Behavior

The library should gracefully handle locations without altitude data. Latitude and longitude should still be returned successfully, with altitude fields set to null.

Suggested Fix

Check hasAltitude() before calling addMslAltitudeToLocation():

internal suspend fun Location.toModel(context: Context): dev.jordond.compass.Location {
    return withContext(Dispatchers.IO) {
        // Only attempt MSL altitude conversion if the location has altitude data
        if (hasAltitude()) {
            try {
                addMslAltitudeToLocation(context, this@toModel)
            } catch (e: Exception) {
                // Log warning but don't fail the entire location request
            }
        }
        // ... rest of conversion
    }
}

Environment

  • Compass version: 3.0.0
  • Platform: Android (tested on API 34)
  • Device: Physical device using network-based location (not GPS)

Reproduction

  1. Disable GPS on an Android device (or use network-only location)
  2. Call MobileGeolocator().current()
  3. Observe GeolocatorResult.Error with message "Missing altitude above WGS84"

Workaround

Currently, forcing GPS/high-accuracy mode may provide altitude data and avoid the error, but this shouldn't be required for basic lat/lng geolocation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions