3 回答

TA貢獻(xiàn)1833條經(jīng)驗(yàn) 獲得超4個(gè)贊
使用此方法獲取最近的已知位置:
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;
}

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超8個(gè)贊
試試這個(gè)對(duì)我有用:-
LocationManager mLocationManager;
Location myLocation = getLastKnownLocation();
private Location getLastKnownLocation() {
Location oldLoc;
while (true){
oldLoc = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (oldLoc == null){
continue;
}
else {
break;
}
}
return oldLoc;
}
您可以使用NETWORK_PROVIDER代替GPS_PROVIDER以獲得更快的結(jié)果。
- 3 回答
- 0 關(guān)注
- 605 瀏覽
添加回答
舉報(bào)