getLastKnownLocation使用GPS_PROVIDER和NETWORK_PROVIDER返回NULL [英] getLastKnownLocation return NULL using GPS_PROVIDER and NETWORK_PROVIDER

查看:332
本文介绍了getLastKnownLocation使用GPS_PROVIDER和NETWORK_PROVIDER返回NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的GPSTracker构造函数类:

This is my GPSTracker constructor class:

    public GPSTracker(Context context) {
    this.mContext = context;
    locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

    Location gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    Location networkLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}

gpsLocation和networkLocation始终为NULL.

gpsLocation and networkLocation are always NULL.

getLastKnowLocation方法不会返回我任何位置...即使我确定在午餐活动(并创建GPSTracker对象)之前几秒钟该设备具有网络位置(在检查了网络位置后,我已禁用了地理标记可用)

getLastKnowLocation method doesn't return me any Location... even if I'm sure that few second before lunching activity (and creating GPSTracker object) the device had network location (I've disabled geotagging after checking that a network location was available)

在带有Android Studio的Nexus 5上进行了测试

Tested on Nexus 5 with Android Studio

推荐答案

是的,getLastKnownLocation()可能返回null. 文档说:

Yep, getLastKnownLocation() may well return null. The documentation says:

返回一个位置,该位置指示来自最后一个已知位置的数据 从给定的提供程序获得的修复程序.

Returns a Location indicating the data from the last known location fix obtained from the given provider.

这可以在不启动提供程序的情况下完成.注意这个 位置可能已过时,例如,如果设备已打开 然后移到另一个位置.

This can be done without starting the provider. Note that this location could be out-of-date, for example if the device was turned off and moved to another location.

如果当前禁用了提供程序,则返回null.

If the provider is currently disabled, null is returned.

Android设备不会一直自动跟踪其位置.仅当某些应用程序请求该位置时,最后知道的位置"才可用.因此,您不应期望总是使用getLastKnownLocation()来获得位置.即使返回了某些内容,该位置也可能不是最新的. Location对象具有可以使用getTime()方法读取的时间戳和可以通过getAccuracy()方法读取的估计精度.这些可以用来评估可用的位置.

An Android device does not automatically track its location all the time. The "last known location" is available only if some application has requested the location. So you should not expect to always get a location with getLastKnownLocation(). And even if it returns something the location might not be up-to-date. The Location object has a timestamp which you can read with the getTime() method and an estimated accuracy which you can read with the getAccuracy() method. These can be used to evaluate is the location usable.

如果您收到的位置为空或非常旧的位置,则需要请求一个新的位置.基本选项只是请求单个位置更新或以您可以指定的时间间隔进行连续更新.

If you receive null or a very old location you'll need to request a fresh location. The basic options are just requesting a single location update or continuous updates with a time interval that you can specify.

无论哪种情况,您的类都需要实现 LocationListener 接口:

In either case your class needs to implement the LocationListener interface:

public class GpsTracker implements LocationListener {

    // ...other code goes here...

    @Override
    public void onLocationChanged(Location location) {

        // Do something with 'location' here.   

    }
}

对于单个位置更新,您可以调用[LocationManager.requestSingleUpdate()]( http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(java.lang.String ,long,float,android.location.LocationListener)).无论哪种情况,都会有一个很小的(或不是那么小的)延迟,并且

For a single location update you would call [LocationManager.requestSingleUpdate()](http://developer.android.com/reference/android/location/LocationManager.html#requestSingleUpdate(java.lang.String, android.location.LocationListener, android.os.Looper)) and for continuous updates [LocationManager.requestLocationUpdates()](http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(java.lang.String, long, float, android.location.LocationListener)). In either case there will be a small (or not so small) delay and the LocationListener.onLocationChanged() callback is called when a location is available.

例如,您可以像这样向GPS提供商请求一次更新:

For example you could request a single update from the GPS provider like this:

locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, this, null);

要从GPS提供商处获取最大持续更新.每隔1秒,并且仅当位置变化至少1米时,您才能致电:

For continuous updates from the GPS provider max. every 1 second and only when the location has changed at least 1 meter you could call:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);

然后还有requestSingleUpdate()的更多变体,您可以在文档中找到,另一方面,您可以在Google Play服务位置API中看到这些变体,但请不要去那里.您的问题是关于getLastKnownLocation()返回null的问题,并且Stack Overflow提供了一些示例,这些示例提供了不同的方法来请求最新的位置.

And then there are some more variations of the requestSingleUpdate() which you can see in the documentation and on the other hand the Google Play services location APIs but let's not go there. Your question was about getLastKnownLocation() returning null and Stack Overflow has several examples of different ways to request an up-to-date location.

编辑:这是一个旧答案.基本想法仍然有效,但是请查看Android文档以获取请求位置更新的现代方法.

This is an old answer. The basic idea is still valid, but check out the Android documentation for modern ways to request location updates.

这篇关于getLastKnownLocation使用GPS_PROVIDER和NETWORK_PROVIDER返回NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆