getLastKnownLocation 返回 null [英] getLastKnownLocation returns null
问题描述
我已经阅读了一些关于此的问题,但我没有完全找到我需要的答案.
I've read some questions about this, but I didn't quite find an answer that I needed.
所以,我的地图已经设置好了,我想获取我当前的 GPS 位置.我已经检查过我的变量不是 NULL 但我的结果来自:
So, the case is that I have my map set up, and I want to grab my current gps location. I have checked that my variables is not NULL but yet my result from:
getLastKnownLocation(provider, false);
虽然给了我空值,所以这就是我需要帮助的地方.我已经为 COARSE + FINE 位置添加了权限.但是我通常会为我的手机禁用各种网络数据,因为我对电话帐单中不可预测的数据流费用不太满意.所以我只启用并连接了 WiFi 以进行此测试.
Gives me null though, so this is where I need help. I have added permissions for COARSE + FINE location. But I usually have all kinds of network data disabled for my phone, because I'm not very happy about unpredictable dataflow expenses in my phone bill. So I have only WiFi enabled and connected for this test.
为了使这成为可能,我还需要启用什么吗?我认为 WiFi 应该足够了?
Is there anything more I NEED to enable in order for this to be possible? I would think WiFi should be sufficient?
推荐答案
使用此方法获取最后的已知位置:
Use this method to get the last known location:
LocationManager mLocationManager;
Location myLocation = getLastKnownLocation();
private Location getLastKnownLocation() {
mLocationManager = (LocationManager)getApplicationContext().getSystemService(LOCATION_SERVICE);
List<String> providers = mLocationManager.getProviders(true);
Location bestLocation = null;
for (String provider : providers) {
Location l = mLocationManager.getLastKnownLocation(provider);
if (l == null) {
continue;
}
if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
// Found best last known location: %s", l);
bestLocation = l;
}
}
return bestLocation;
}
这篇关于getLastKnownLocation 返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!