【问题标题】:Android GPS takes a while to be accurateAndroid GPS 需要一段时间才能准确
【发布时间】:2012-01-16 16:32:32
【问题描述】:

我制作了一个 Android 应用程序,通过单击按钮获取经度和纬度位置。

起初我得到最后一个已知的位置读数,为了论证,当我第一次加载应用程序/打开 gps 时,它是不准确的。

我想知道的是如何等待它准确无误,就像在 Google 地图中当您收到 toast 消息“等待位置”时一样。

如果您看到任何可以改进代码的方法,那也会很有帮助。

参考代码:

public class Clue extends Activity {

    public static double latitude;
    public static double longitude;
    Criteria criteria;
    LocationManager lm;
    LocationListener ll;
    Location location;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.questions);

      criteria = new Criteria();
      criteria.setAccuracy(Criteria.ACCURACY_FINE);
      lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
      ll = new MyLocationListener();
      lm.requestLocationUpdates(lm.getBestProvider(criteria, true), 0, 0, ll);          
}



private boolean weAreThere() {
    location = getLocation(); 
    longitude = location.getLongitude();
    latitude = location.getLatitude();

    return inCorrectPlace(param);
}

private Location getLocation() {
     lm.requestLocationUpdates(lm.getBestProvider(criteria, true), 0, 0, ll); 
      return lm.getLastKnownLocation(lm.getBestProvider(criteria, true));
}
}

    public class MyLocationListener implements LocationListener
    {
        public void onLocationChanged(Location loc)
        {        
             Clue.longitude = loc.getLongitude();
             Clue.latitude = loc.getLatitude();
        }
}

感谢您的阅读,我们将不胜感激。

本

【问题讨论】:

  • 是否考虑过使用所有提供者而不是最好的提供者?它不必非常准确。

标签: java android gps location locationmanager


【解决方案1】:

如果 Location.hasAccuracy() 返回 true,您可以调用 Location.getAccuracy() 以获取以米为单位的精度,然后过滤您认为不够准确的那些。

请注意,您没有使用LocationManager.GPS_PROVIDER,因此您的修复也可以通过其他方式(如 WiFi)获得。

在获取 GPS 修复(户外)之前,通常预计 GPS 芯片会“变热”* 大约一分钟。

*我所说的热点是指有卫星覆盖。一些芯片组在一段时间后断开连接(“冷”)以保存电池。芯片冷时,首次修复时间 (TTFF) 会更长。

【讨论】:

    【解决方案2】:

    这里有一个很好的解释,

    http://android-developers.blogspot.com/2011/06/deep-dive-into-location.html

    帮助您准确了解如何实现它。

    【讨论】:

      【解决方案3】:

      最初,你应该使用getlastknownLocation ()来修复

      此外,您可以使用后台服务,使用 NETWORK.PROVIDER 更新位置...如果使用得当,不会耗尽您的电池...并且在 GPS 关闭时也可以工作

      如果没有网络提供商,那么你应该尝试让用户打开 GPS。

      如果你使用 2.2 试试PASSIVE PROVIDER

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-05-04
        • 2015-12-23
        • 1970-01-01
        • 2022-06-22
        • 1970-01-01
        • 2019-12-15
        • 1970-01-01
        • 2013-07-25
        相关资源
        最近更新 更多