Android 从 SDK 上的“getLastKnownLocation”获取空值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1916568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting null from 'getLastKnownLocation' on SDK
提问by AndroiDBeginner
I have a problem related to the Location API.
我有一个与位置 API 相关的问题。
I tried the following code:
我尝试了以下代码:
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location loc = getLastKnownLocation(LocationManager.GPS_PROVIDER);
loc
is always null
, when getLastKnownLocation()
is called.
loc
is always null
,何时getLastKnownLocation()
被调用。
What is wrong?
怎么了?
回答by Anthony Forloney
Along with the permissions in your AndroidManifest.xml
file, have you registered a location listener?
除了AndroidManifest.xml
文件中的权限外,您是否注册了位置侦听器?
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location loc = getLastKnownLocation(LocationManager.GPS_PROVIDER);
lm.requestLocationUpdates(LocationManager.GPS, 100, 1, locationListener);
Then have a method, in this case locationListener
, to complete your task
然后有一个方法,在这种情况下locationListener
,完成你的任务
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
回答by Erich Douglass
回答by xarlymg89
I had the same problem as you, I always receive a null Location object, But finally it was solved in an easy way. You must have a valid GPS location, so, if the GPS is not enabled and you dont't have enough signal, the Location object will be ALWAYS null.
我和你有同样的问题,我总是收到一个空的 Location 对象,但最后它以一种简单的方式解决了。您必须拥有有效的 GPS 位置,因此,如果未启用 GPS 且您没有足够的信号,则 Location 对象将始终为空。
回答by Dan Lew
Have you set the permissions in your AndroidManifest.xml? You need these permissions in order to access the user's location with an application:
您是否在 AndroidManifest.xml 中设置了权限?您需要这些权限才能使用应用程序访问用户的位置:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
回答by OWADVL
Here's something you can use:
您可以使用以下内容:
LocationManager lm = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = lm.getBestProvider(criteria, false);
Location loc = lm.getLastKnownLocation(bestProvider);
last_lat = loc.getLatitude();
last_lng = loc.getLongitude();
回答by Gilberto Ibarra
You need the instance Of LocationManager like this:
您需要像这样的 LocationManager 实例:
First Instance:
第一个例子:
LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Wrong:
错误的:
Location loc = getLastKnownLocation(LocationManager.GPS_PROVIDER);
Correct:
正确的:
Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);