我的 Android 程序使用 GPS 非常不准确

发布于 2024-12-20 03:19:32 字数 1072 浏览 2 评论 0原文

我使用 GPS 编写了一个程序,其纬度和经度相差大约 8 英里。

我想这可能是因为我在里面,但是当我在手机上使用谷歌地图时,它显示我在我家。

知道为什么会这样吗?

下面的代码:

public class Clue extends Activity {

public static double latitude;
public static double longitude;
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.blah);

  Criteria criteria = new Criteria();
  criteria.setAccuracy(Criteria.ACCURACY_FINE);

  lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
  ll = new MyLocationListener()}

private boolean getLoc() {
location = getLocation(); 

   longitude = location.getLongitude();
   latitude = location.getLatitude();
return inCorrectPlace(params);
}


    private Location getLocation() {
  lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);   
  return lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
}

我已经删除了不相关的代码,所有帮助将不胜感激。

I have made a program using GPS and its Latitude and Longitude are about 8 miles off.

I thought it may be because I am inside but when I use google maps on my phone, it shows me at my house.

Any idea why this may be?

Code below:

public class Clue extends Activity {

public static double latitude;
public static double longitude;
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.blah);

  Criteria criteria = new Criteria();
  criteria.setAccuracy(Criteria.ACCURACY_FINE);

  lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
  ll = new MyLocationListener()}

private boolean getLoc() {
location = getLocation(); 

   longitude = location.getLongitude();
   latitude = location.getLatitude();
return inCorrectPlace(params);
}


    private Location getLocation() {
  lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);   
  return lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
}

I have stripped out irrelevant code, all help will be appreciated.

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

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

发布评论

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

评论(2

沉睡月亮 2024-12-27 03:19:32

您所犯的主要错误是认为您可以立即请求准确的位置修复。调用 getLastKnownLocation() 会为您提供一个可能相当旧的位置。您必须首先告诉手机开始尝试获取位置修复。这需要时间,因此您会异步接收位置更新。

您需要做的是调用 requestLocationUpdates(),然后在 LocationListener 类的 onLocationChanged() 处理程序中接收更新。不一定会立即收到位置更新。可能需要几秒钟到几分钟。

请参阅 Android 开发指南 http://developer.android.com/ Guide/topics/location/obtaining-user-location.html

the key mistake you're making is thinking that you can instantaneously request an accurate location fix. calling getLastKnownLocation() gives you a location that could be quite old. you have to first tell the phone to start to try to get a location fix. this takes time, so you receive the location update asynchronously.

what you need to do is call requestLocationUpdates(), then receive the updates in the onLocationChanged() handler in your LocationListener class. the location updates will not necessarily be received right away. it could take several seconds to several minutes.

see the android dev guide http://developer.android.com/guide/topics/location/obtaining-user-location.html

朱染 2024-12-27 03:19:32

您不能真正依赖 getLastKnownLocation 来保证其可靠性。它可能已过时、不准确或为空。

您应该做的是使用 requestLocationUpdates 主动获取修复。

至于为什么谷歌地图会修复您的位置,它可能正在使用网络提供商,而您只检查最后一次 GPS 修复。

You can't really rely on getLastKnownLocation to be reliable. It could be out of date, inaccurate, or null.

What you should do is use requestLocationUpdates to actively get a fix.

As far as why google maps is fixing your location, it might be using the Network provider while you are only checking the last GPS fix.

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