getLastKnownLocation返回null [英] getLastKnownLocation returns null

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

问题描述

我读过有关这方面的一些问题,但我并没有完全找到,我需要一个答案。

I've read some questions about this, but I didn't quite find an answer that I needed.

因此​​,本案是,我有我的地图设置,我要抢我的当前GPS位置。 我检查了我的变量不为空,但还没有从我的结果是:

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);

让我空了,所以这是我需要帮助。 我已经加入粗+精确的位置信息的权限。但我通常有各种我的电话禁用网络数据的,因为我不是很高兴联合国predictable数据流的费用在我的手机话费。所以我只支持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.

有没有什么更需要启用为了让这成为可能?我想无线应该是足够了吗?

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屋!

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