返回的位置是空当供应商是GPS?空当、位置、供应商、GPS

2023-09-08 08:35:31 作者:小心我非礼你

我试图找回在下面这段code中的我现在的位置坐标

I am trying to retrieve the my current location coordinates in the following piece of code

LocationManager locationManager= (LocationManager) getSystemService(context);

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);

provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);

我得到的位置坐标,当供应商是的网络的。

当我改变位置和安全选项的设置来启用使用GPS卫星选项提供者更改为 GPS 的。 在这种情况下返回的位置为空。

When I change the settings in the location and security options to enable "Use GPS satellites" option the provider changes to "gps". In this case the location returned is null.

还有什么能这样做的原因,怎么能解决?

What could be the cause of this and how can it be rectified?

推荐答案

这需要一段时间,为GPS提供者得到修复,特别是如果没有previous修复程序进行。该getLastKnownLocation不是一个阻塞调用,它将等待,直到一个实际的现场GPS定位的确立(即修正可能需要长达1分钟,这取决于不同的原因)。

It takes a while for the GPS provider to get a fix, especially if no previous fix was made. The getLastKnownLocation is not a blocking call that will wait untill an actual live GPS fix is established (that fix can take up to 1 min, depending on various reasons).

另外,还要确保你在室外原因很明显)。

Also, make sure you're outside for obvious reasons).

而不是简单地调用getLastKnownLocation为GPS提供者,你首先需要请求它的位置更新。

Instead of simply calling the getLastKnownLocation for the GPS provider, you first need to request location updates from it.

下面的文章解释了原理非常好:的http://developer.android.com/guide/topics/location/obtaining-user-location.html

The following article explains the principle very well : http://developer.android.com/guide/topics/location/obtaining-user-location.html

这是一个必须阅读任何人这样做对Android的位置提供什么。

It's a must read for anyone doing anything with the location provider on Android.

典型的流程是:

启动应用程序。 开始监听来自期望的位置提供更新。 将筛选出新的,但不太精确的修复维护位置的当前最佳估计数。 停止监听位置更新。 取最后的最佳位置估计的优势。

看着下面的code,我们正在初始化MyLocationListener(追踪手机的位置),并检索引用的LocationManager。我们要求从locationmanager位置更新。我们使用35秒minDistance依旧(对于通知的最小距离间隔)为10米,一个minTime(的最小时间间隔的通知)。

Looking at the code below, we’re initializing a MyLocationListener (to track the phone’s position), and retrieving a references to the LocationManager. We’re requesting location updates from the locationmanager. We’re using a minDistance( minimum distance interval for notifications) of 10 meters, and a minTime (the minimum time interval for notifications) of 35 seconds.

LocationListener locationListener = new MyLocationListener();
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, this.locationListener);

在实施这一点,你可能会注意到,MyLocationListener接收更多的更新,那么你预期(考虑到minTime和minDistance依旧参数)。下面的文章在的http://blog.doityourselfandroid.com/2010/12/25/understanding-locationlistener-android/可以帮助你理解为什么。

When implementing this, you'll probably notice that the MyLocationListener receives more updates then you expected (given the minTime and minDistance parameters). The following article at http://blog.doityourselfandroid.com/2010/12/25/understanding-locationlistener-android/ can help you understand why.