我正在尝试使用OldCompany
获取用户位置,如下所示:
FusedLocationProviderClient
我在此位置调用中获取位置更新
fusedLocationProviderClient.requestLocationUpdates(mLocationRequest, locationCallback, null);
我读到 locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
for (Location location : locationResult.getLocations()) {
// list of locations
}
}
};
检索从最旧到最新排序的位置对象列表,
我无法理解我想要的只是获取用户位置。
对此有何帮助?
答案 0 :(得分:0)
您可以使用locationResult.getLastLocation()
获取最新的可用位置。
来自文档:LocationResult.getLastLocation()
public Location getLastLocation ()
Returns the most recent location
available in this result, or null if no
locations are available.
答案 1 :(得分:0)
您可以使用LocationManager
代替FusedLocationProviderClient
。它可能会更容易。
LocationManager manager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.d("Current location", location.toString());
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
//Checks permission
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
这将在用户移动时为您提供位置。