Android GPS 需要一段时间才能准确

发布于 2024-12-20 12:17:45 字数 1587 浏览 2 评论 0原文

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

首先,我得到了最后一个已知的位置读数,出于争论的原因,当我第一次加载应用程序/打开 GPS 时,这是不准确的。

我想知道的是如何等待它准确,就像在谷歌地图中,​​当你收到“等待位置”的吐司消息时。

如果您发现可以改进代码的任何方法,它也会很有帮助。

参考代码:

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();
        }
}

感谢您的阅读,所有回复将不胜感激。

I have made an Android app that gets location by longitude and latitude on a button click.

At first I get the last known location reading, which is, for argument sake, inaccurate when I first load up the app/turn on gps.

What I would like to know is how to wait for it to be accurate, like in Google maps when you get the toast message 'waiting for location'.

If you see any way the code can be improved it would also be helpful.

Code for reference:

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();
        }
}

Thanks for reading, all replies will be appreciated.

Ben

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

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

发布评论

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

评论(3

稚然 2024-12-27 12:17:45

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

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

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

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

If Location.hasAccuracy() returns true, you could call Location.getAccuracy() to retrieve the accuracy in meters, then filter the ones you don't consider enough accurate.

Notice you are not using LocationManager.GPS_PROVIDER so your fixes could also be obtained by other means (like WiFi).

Usually expect about a minute for the GPS chip to get "hot"* before getting GPS fixes (outdoors).

*By hot I mean having satellites coverage. Some chipsets get disconnected ("cold") after some time to preserve battery. The Time To First Fix (TTFF) is greater when the chip is cold.

亣腦蒛氧 2024-12-27 12:17:45

这是一个很好的解释,

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

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

Here is an excellent explanation,

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

helps you understand exactly how to implement it.

A君 2024-12-27 12:17:45

最初,您应该使用 getlastknownLocation () 进行修复。

此外,您可以使用使用 NETWORK.PROVIDER 更新位置的后台服务...如果明智地使用,它不会耗尽您的电池...而且如果 GPS 关闭,则可以工作

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

如果您使用 2.2,请尝试 被动提供商

Initially , u should get fix using getlastknownLocation ()

In addition, U can use a background service that updates location using NETWORK.PROVIDER... which shall not drain ur battery if used judiciously...and also works if GPS is turned off

If there is no network provider, then u should try and ask user to turn on GPS.

If u r using 2.2 try PASSIVE PROVIDER

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