为什么Location.getAltitude()总是返回零,至少在模拟器中?

时间:2012-06-15 16:04:47

标签: android gps

有人知道为什么我在下面的getAltitude总是返回0?

package com.example.helloandroid;

import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

public class HelloAndroid extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d("main", "onCreate");
        setupGps();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    LocationListener locationListener;
    LocationManager lm;

    void setupGps() {
        Log.d("gps", "Setting up GPS...");
        locationListener = new MyLocationListener();
        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 20000, 5,
                locationListener);

        Log.d("gps",
                "GPS supports altitude: "
                        + lm.getProvider(LocationManager.GPS_PROVIDER)
                                .supportsAltitude());
        Log.d("gps", "Finished setting up GPS.");
    }

    static class MyLocationListener implements LocationListener {

        public void onLocationChanged(Location location) {
            Log.d("gps", "onLocationChanged");
            Log.d("gps",
                    "x: " + location.getLongitude() + ", y: "
                            + location.getLatitude() + ", alt.: "
                            + location.getAltitude());
        }

        public void onProviderDisabled(String provider) {
            Log.d("gps", "onProviderDisabled");
        }

        public void onProviderEnabled(String provider) {
            Log.d("gps", "onProviderEnabled");
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.d("gps",
                    "onStatusChanged; new status: " + String.valueOf(status));
        }
    }
}

要进行测试,我会向模拟器发出一个geo高度命令:

[someone@somewhere ~]$ telnet localhost 5554
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Android Console: type 'help' for a list of commands
OK
geo fix -121.45356 46.51119 4392
OK

getAltitude()返回0:

enter image description here

我只尝试使用模拟设备。

修改

跟进cheeken的评论,我确认Location.hasAltitude()是真的。它是。还有其他想法吗?

public void onLocationChanged(Location location) {
    Log.d("gps", "onLocationChanged");
    Log.d("gps", "x: " + location.getLongitude() 
        + ", y: " + location.getLatitude());
    Log.d("gps", "hasAltitude: " + location.hasAltitude() 
        + ", alt.: " + location.getAltitude());
}

enter image description here

1 个答案:

答案 0 :(得分:1)

似乎很多人都遇到了类似的问题,甚至被报道为possible bug。如果模拟器此时根本不支持它,我不会感到惊讶。您可能需要使用真实设备。