为什么Location.getAltitude()总是返回为零,至少在模拟器? [英] Why does Location.getAltitude() always return zero, at least in the emulator?

查看:2198
本文介绍了为什么Location.getAltitude()总是返回为零,至少在模拟器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Does anybody know why my getAltitude in the following always returns 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:

[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:

我只试过与模拟的设备。

I have only tried with emulated devices.

修改

在cheeken的评论之后,我验证了 Location.hasAltitude()是真实的。它是。任何其他的想法?

Following up on cheeken's comment, I verified that Location.hasAltitude() is true. 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.

这篇关于为什么Location.getAltitude()总是返回为零,至少在模拟器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆