从 Location 对象获取卫星数量

发布于 2024-11-27 19:36:20 字数 367 浏览 5 评论 0原文

我正在使用 GPS 提供商和 LocationListener.onLocationChanged(Location location) 来接收位置修复。
文档说,Location.getExtras()包含下一个键/值对:

卫星 - 用于得出定位结果的卫星数量

,但在实践中我得到一个空的额外对象 - 那里没有任何数据。
这是否意味着我得到的是 A-GPS 修复而不是 GPS?

I'm using a GPS provider and LocationListener.onLocationChanged(Location location) to receive location fixes.
Documentation says, that Location.getExtras() contains next key/value pair:

satellites - the number of satellites used to derive the fix

but on practice I'm getting an empty extra object - there is no any data there.
Does it means that I'm getting the A-GPS fixes and not GPS?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

远山浅 2024-12-04 19:36:20

要获取 GPS 引擎使用的卫星数量,您需要实现 android.location.GpsStatus.Listener 并实现其方法 onGpsStatusChanged()

例子...

public void onGpsStatusChanged(int event) {
    int satellites = 0;
    int satellitesInFix = 0;
    int timetofix = locationManager.getGpsStatus(null).getTimeToFirstFix();
    Log.i(TAG, "Time to first fix = " + timetofix);
    for (GpsSatellite sat : locationManager.getGpsStatus(null).getSatellites()) {
        if(sat.usedInFix()) {
            satellitesInFix++;              
        }
        satellites++;
    }
    Log.i(TAG, satellites + " Used In Last Fix ("+satellitesInFix+")"); 
}

To get the number of satellites used by the GPS engine you need to implement android.location.GpsStatus.Listener and implement its method onGpsStatusChanged().

Example...

public void onGpsStatusChanged(int event) {
    int satellites = 0;
    int satellitesInFix = 0;
    int timetofix = locationManager.getGpsStatus(null).getTimeToFirstFix();
    Log.i(TAG, "Time to first fix = " + timetofix);
    for (GpsSatellite sat : locationManager.getGpsStatus(null).getSatellites()) {
        if(sat.usedInFix()) {
            satellitesInFix++;              
        }
        satellites++;
    }
    Log.i(TAG, satellites + " Used In Last Fix ("+satellitesInFix+")"); 
}
幸福%小乖 2024-12-04 19:36:20

我使用 Location.getExtras().getInt("satellites"),它给出了正在使用的卫星数量。

I use Location.getExtras().getInt("satellites"), and it give the number of satellites in use.

囍笑 2024-12-04 19:36:20

由于 Android API 24 GpsStatus 已弃用,因此应使用 GnssStatus。让我们有一个处理 Gps 数据的活动或服务以及一个已经创建的 LocationManager

private GnssStatus.Callback gnssCallback;

public void initCallbacks() {
    ....
    gnssCallback = new GnssStatus.Callback() {
        @Override
        public void onSatelliteStatusChanged(@NonNull GnssStatus status) {
            final int satelliteCount = status.getSatelliteCount();
            int usedCount = 0;
            for (int i = 0; i < satelliteCount; ++i)
                if (status.usedInFix(i))
                    ++usedCount;
            Log.d("MyServiceTag", "satellites count = " + satelliteCount + ", used = " + usedCount);
        }
    };
    locationManager.registerGnssStatusCallback(gnssCallback, new Handler(Looper.myLooper()));
    ....
}

public void deinitCallbacks() {
    ....
    locationManager.unregisterGnssStatusCallback(gnssCallback);
    ....
}

initCallbacks() 应在 locationManager 初始化后调用。当不再需要卫星数量信息时(例如在onDestroy() 中),应调用deinitCallbacks()GnssStatus.getSatelliteCount() 返回已知卫星的总数,GnssStatus.usedInFix(int i) 告诉在最实际的位置捕获中是否使用了第 i 颗卫星。

Since Android API 24 GpsStatus is deprecated and one should use GnssStatus. Let us have an activity or a service processing Gps data and a LocationManager already created.

private GnssStatus.Callback gnssCallback;

public void initCallbacks() {
    ....
    gnssCallback = new GnssStatus.Callback() {
        @Override
        public void onSatelliteStatusChanged(@NonNull GnssStatus status) {
            final int satelliteCount = status.getSatelliteCount();
            int usedCount = 0;
            for (int i = 0; i < satelliteCount; ++i)
                if (status.usedInFix(i))
                    ++usedCount;
            Log.d("MyServiceTag", "satellites count = " + satelliteCount + ", used = " + usedCount);
        }
    };
    locationManager.registerGnssStatusCallback(gnssCallback, new Handler(Looper.myLooper()));
    ....
}

public void deinitCallbacks() {
    ....
    locationManager.unregisterGnssStatusCallback(gnssCallback);
    ....
}

initCallbacks() should be called after locationManager initialization. deinitCallbacks() should be called when information on the number of satellites is no longer needed, e.g. in onDestroy(). GnssStatus.getSatelliteCount() returns total number of known satellites, GnssStatus.usedInFix(int i) tells whether i-th satellite had been used in the most actual location capture.

倥絔 2024-12-04 19:36:20

不,这意味着您的手机制造商决定不实施此操作。 (或者您可以使用不使用卫星的NETWORK_PROVIDER

使用NmeaListener 并解析句子以了解可见或使用的卫星数量。

Nope it means that your phone manufacturer decided not to implement this. (Or you could be using the NETWORK_PROVIDER which does not use satellites)

Use a NmeaListener and parse the sentences to know the number of satellites visible or used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文