【问题标题】:Android GpsStatus always has zero satellitesAndroid GpsStatus 始终有零卫星
【发布时间】:2017-01-14 06:23:50
【问题描述】:

我正在尝试获取 GPS 定位中使用的卫星以获取更多信息,但 GpsStatus.getSatellites() 方法总是返回一个空迭代器,即使我可以很好地获取该位置的纬度/经度/高度。

LocationManager locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

if (location != null) {

    //get location coordinates
    mLatitude = location.getLatitude();
    mLongitude = location.getLongitude();
    double altitude = location.getAltitude();
    Log.d("test2", "lat : " + mLatitude);
    Log.d("test2", "long : " + mLongitude);

    //get satellite data
    GpsStatus gpsStatus = locationManager.getGpsStatus(null);

    int satellites = 0;
    int satellitesInFix = 0;
    int timetofix = gpsStatus.getTimeToFirstFix();
    Log.d("test2", "Time to first fix = " + timetofix);
    for (GpsSatellite sat : gpsStatus.getSatellites()) {
        if(sat.usedInFix()) {
            satellitesInFix++;
        }
        satellites++;
    }
    Log.d("test2", "Satellites: " + satellitesInFix + "/" + satellites);
}

我在调试日志中得到的只是以下内容,一遍又一遍:

09-06 18:42:48.093 6842-6842/com.android.example.gpstest D/test2: lat : 39.509401143731274
09-06 18:42:48.093 6842-6842/com.android.example.gpstest D/test2: long : -9.064780960816815
09-06 18:42:48.094 6842-6842/com.android.example.gpstest D/test2: Time to first fix = 0
09-06 18:42:48.095 6842-6842/com.android.example.gpstest D/test2: Satellites: 0/0

我已经尝试过使用多种设备,认为特定型号可能有些怪癖,但它们都有相同的问题,所以我的代码似乎不正确。我需要做什么才能获得卫星?

【问题讨论】:

    标签: java android gps android-location android-gps


    【解决方案1】:

    因为您还没有启动 GPS。 getLastKnownLocation 不会打开 GPS,它只返回一个缓存值(如果有)。您需要请求LocationUpdates 才能这样做。

    【讨论】:

    • 我在活动的 onResume 方法上调用 requestLocationUpdates(),我发布的代码在 onLocationChanged 方法中。
    • 你为什么会在 onLocationChanged 中调用 getLastKnownLocation?它会传递给您最后一个位置
    • 另外,来自 getGpsStatus 文档:“检索有关 GPS 引擎当前状态的信息。这只能从 onGpsStatusChanged(int) 回调中调用,以确保自动复制数据。调用者可以传入一个 GpsStatus 对象来设置最新的状态信息,或者传递 null 来创建一个新的 GpsStatus 对象。”
    • 啊,可能是这样。但是,我刚刚实现了 onGpsStatusChanged,它没有捕获任何状态更改事件,即使我禁用和启用位置服务也是如此。
    • 你调用过 addGpsStatusListener() 吗?
    猜你喜欢
    • 2012-05-11
    • 2011-12-04
    • 2013-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多