安卓系统。GPS从细到粗的回落[英] Android: GPS fallback from fine to coarse

本文是小编为大家收集整理的关于安卓系统。GPS从细到粗的回落的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

问候,

当我没有修复时,我知道我如何获得粗大的GPS坐标并在我有修复时获得良好的GPS坐标?

我已经尝试搜索一些示例代码,但无济于事.

我确实找到了这个: httpp ://www.android10.org/index.php/articleslocationmaps/226-android-location-providers-gps-network-passive

,但我不知道如何将后备实现为粗/升级.

我希望有人能提供帮助.预先感谢,

推荐答案

您可以在文档.基本想法是,您可以聆听来自不同提供商的更新.收到新位置时,您将其与以前的存储位置进行比较(上述链接中提供了示例功能).

位置对象具有getAccuracy,您可以用来测量其精度.您还应该设置一个计时器,以便您知道位置提供商提供了更新后已经过去了多长时间.如果在GPS提供商给您更新后已经过去了两分钟以上,请开始收听网络更新.在收听网络更新时,如果GPS为您提供了新的更新,请切换到FINE位置.

其他推荐答案

您可以使用以下代码获得最后一个知道的位置.它使位置提供商和循环在阵列后退.即以GPS开头,如果没有GPS,则获得网络位置.您可以在需要获取位置时调用此方法.

private double[] getGPS() {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
List<String> providers = lm.getProviders(true);

/* Loop over the array backwards, and if you get an accurate location, then break         out the loop*/
Location l = null;

for (int i=providers.size()-1; i>=0; i--) {
    l = lm.getLastKnownLocation(providers.get(i));
    if (l != null) break;
}

double[] gps = new double[2];
if (l != null) {
    gps[0] = l.getLatitude();
    gps[1] = l.getLongitude();
}
return gps;
}

本文地址:https://www.itbaoku.cn/post/102458.html

问题描述

Greetings,

Does anyone know how I can get coarse GPS coordinates when I don't have a fix and get fine GPS coordinates when I have a fix?

I've tried googling for some sample code to no avail.

I did find this: http://www.android10.org/index.php/articleslocationmaps/226-android-location-providers-gps-network-passive

But I don't know how to implement the fallback to coarse/upgrade to fine.

I hope someone can help. Thanks in advance,

推荐答案

You can find an excellent introduction to the subject in the documentation. The basic idea is that you enable listening for updates from different providers. When a new location is received, you compare it to the previous stored location (a sample function is provided in the above link).

A location object has an getAccuracy that you can use to measure its accuracy. You should also set up a timer so that you know how long has passed after a location provider has provided an update. If more than two minutes have passed after GPS provider has given you an update, then start listening for network updates. While listening for network updates, if GPS gives you a new update, then switch to fine location.

其他推荐答案

You can get the last know location using the code below. It gets the location providers and loops over the array backwards. i.e starts with GPS, if no GPS then gets network location. You can call this method whenever you need to get the location.

private double[] getGPS() {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
List<String> providers = lm.getProviders(true);

/* Loop over the array backwards, and if you get an accurate location, then break         out the loop*/
Location l = null;

for (int i=providers.size()-1; i>=0; i--) {
    l = lm.getLastKnownLocation(providers.get(i));
    if (l != null) break;
}

double[] gps = new double[2];
if (l != null) {
    gps[0] = l.getLatitude();
    gps[1] = l.getLongitude();
}
return gps;
}