androidandroid-studio

Android Studio Simulator - Simulate Altitude


I'm trying to obtain GPS position in Android Studio's simulator with LocationRequest using PRIORITY_HIGH_ACCURACY and ACCESS_FINE_LOCATION.

location.hasAltitude() returns true but location.altitude always returns 0.0. Is there a way to simulate altitude using the simulator? I even tried using a KML file, with no luck.


Solution

  • Try importing GPX data using the Extended Controls | Location and Import GPX/KML.

    GPX schema supports elevation in the trkpt as in

    <trkpt lat="47.498967521966314" lon="4.413342513063558">
        <ele>365.88000000000005</ele>
        <time>2024-11-15T11:04:08.146Z</time>
    </trkpt>
    

    GPX elevation is reported as altitude in the location data.

    Here's a LocationCallback snippet logging the data:

       locationCallback = new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult locationResult) {
                if (locationResult == null) {
                    return;
                }
                for (Location location : locationResult.getLocations()) {
                    // Update UI with location data
                    // ...
                    Log.d("","Has altitude: "+location.hasAltitude());
                    Log.d("", "location: " + location);
                }
            }
        };
    

    And the log data (including alt data)

    ...  D  Has altitude: true
    ...  D  location: Location[fused 48.225400,4.887958 hAcc=3 et=+8m32s81ms alt=345.3 vel=0.0 bear=303.0 vAcc=1 sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=52]}]
    

    Here's a well-formed, reduced gpx file example used:

    <?xml version="1.0" encoding="UTF-8"?>
    <gpx creator="StravaGPX" version="1.1" xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
     <metadata>
      <name>Tour de France 2024 - Étape 8 - Semur-en-Auxois &gt; Colombey les Deux Églises</name>
      <author>
       <name>Amaury Sport Organisation</name>
       <link href="https://www.strava.com/athletes/7834606"/>
      </author>
      <copyright author="OpenStreetMap contributors">
       <year>2020</year>
       <license>https://www.openstreetmap.org/copyright</license>
      </copyright>
      <link href="https://www.strava.com/routes/3230099028774072386"/>
     </metadata>
     <trk>
      <name>Tour de France 2024 - Étape 8 - Semur-en-Auxois &gt; Colombey les Deux Églises</name>
      <link href="https://www.strava.com/routes/3230099028774072386"/>
      <type>cycling</type>
      <trkseg>
       <trkpt lat="47.498560000000005" lon="4.38862">
        <ele>355.14</ele>
        <time>2024-11-15T11:04:08.146Z</time>
       </trkpt>
       <trkpt lat="47.49856500336607" lon="4.389499999908491">
        <ele>358.71000000000004</ele>
        <time>2024-11-15T11:04:13.245Z</time>
       </trkpt>
      </trkseg>
     </trk>
    </gpx>
    

    Tested on Android Studio Iguana Emulator Pixel 8 Pro API 30


    Note that the location callback rate is independent of the emulator playback rate, particularly since the playback speed can be scaled.

    When the playback is stopped, the reported position and altitude are the last processed values.