3 回答
TA贡献1833条经验 获得超4个赞
使用此方法获取最近的已知位置:
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贡献1785条经验 获得超8个赞
试试这个对我有用:-
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以获得更快的结果。
- 3 回答
- 0 关注
- 572 浏览
添加回答
举报