简体   繁体   English

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

[英]Why does Location.getAltitude() always return zero, at least in the emulator?

Does anybody know why my getAltitude in the following always returns 0?有人知道为什么我在下面的 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));
        }
    }
}

To test, I issue a geo command with an altitude to the emulator:为了进行测试,我向模拟器发出了一个带有海拔高度的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

But getAltitude() returns 0:但是getAltitude()返回 0:

在此处输入图片说明

I have only tried with emulated devices.我只尝试过模拟设备。

Edit编辑

Following up on cheeken's comment, I verified that Location.hasAltitude() is true.跟进丘肯的评论,我确认Location.hasAltitude()是真的。 It is.这是。 Any other idea?还有其他想法吗?

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());
}

在此处输入图片说明

It seems like a lot of people are having similar issues with this and has even been reported as a possible bug .似乎很多人都遇到了类似的问题,甚至被报告为可能的错误 I wouldn't be surprised if the emulator simply doesn't support it at this time.如果模拟器此时根本不支持它,我不会感到惊讶。 You'll probably have to work with a real device.您可能必须使用真实设备。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM