gpt4 book ai didi

android - getLastKnownLocation 返回 null

转载 作者:IT老高 更新时间:2023-10-28 13:15:47 28 4
gpt4 key购买 nike

我已经阅读了一些关于此的问题,但我并没有找到我需要的答案。

所以,情况是我设置了我的 map ,我想获取我当前的 gps 位置。我检查了我的变量不是 NULL,但我的结果来自:

getLastKnownLocation(provider, false);

虽然给我 null,所以这是我需要帮助的地方。我添加了 COARSE + FINE 位置的权限。但是我通常会为我的手机禁用各种网络数据,因为我对手机账单中不可预测的数据流费用不太满意。所以我只为这个测试启用并连接了 WiFi。

我还需要启用什么才能使这成为可能吗?我认为 WiFi 应该足够了?

最佳答案

使用此方法获取最后一个已知位置:

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

关于android - getLastKnownLocation 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20438627/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com