getLastKnownLocation返回nullgetLastKnownLocation、null

2023-09-05 06:58:16 作者:、青春無敵

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

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