I've solved. Here is the code.
locationManager.getCurrentLocation(
LocationManager.GPS_PROVIDER,
null,
application.getMainExecutor(),
new Consumer<Location>() {
@Override
public void accept(Location location) {
// code
}
});
Answer from LaoDa581 on Stack OverflowVideos
I've solved. Here is the code.
locationManager.getCurrentLocation(
LocationManager.GPS_PROVIDER,
null,
application.getMainExecutor(),
new Consumer<Location>() {
@Override
public void accept(Location location) {
// code
}
});
N.B. Below is deprecated in API 30 (it still works at present) - but may be useful for older target devices
In a class that implements LocationListener, e.g. an Activity:
val locationMgr = getSystemService(Context.LOCATION_SERVICE) as LocationManager
...
locationMgr.requestSingleUpdate(LocationManager.GPS_PROVIDER, this, null)
...
// below needed for some API versions ...
override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {}
override fun onLocationChanged(location: Location) {
val lat = location.latitude
val lon = location.longitude
...do your stuff here :)
}
First you need to define a LocationListener to handle location changes.
int LOCATION_REFRESH_TIME = 15000; // 15 seconds to update
int LOCATION_REFRESH_DISTANCE = 500; // 500 meters to update
....
private final LocationListener mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(final Location location) {
//your code here
}
};
Then get the LocationManager and ask for location updates
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_REFRESH_TIME,
LOCATION_REFRESH_DISTANCE, mLocationListener);
}
And finally make sure that you have added the permission on the Manifest,
For using only network based location use this one
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
For GPS based location, this one
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
You need to write code in the OnLocationChanged method, because this method is called when the location has changed. I.e. you need to save the new location to return it if getLocation is called.
If you don't use the onLocationChanged it always will be the old location.
in kotlin any method of the form getX can be written as just x, this is called "property access syntax". There is no separate kotlin version. fusedLocationClient.lastLocation is really exactly the same as fusedLocationClient.getLastLocation(). You can even write this last form in kotlin if you want.
However, this is only true for "get" methods without parameters. The thing is, getCurrentLocation does have parameters so property access syntax is not possible in this case. as you can see here this is the signature of this method:
public Task<Location> getCurrentLocation (int priority, CancellationToken token)
So you should use it like that. for example
fusedLocationClient.getCurrentLocation(LocationRequest.PRIORITY_HIGH_ACCURACY, null)
EDIT:
apparently null as parameter is not allowed. According to https://stackoverflow.com/a/72159436/1514861 this is a possibility:
fusedLocationClient.getCurrentLocation(LocationRequest.PRIORITY_HIGH_ACCURACY, object : CancellationToken() {
override fun onCanceledRequested(p0: OnTokenCanceledListener) = CancellationTokenSource().token
override fun isCancellationRequested() = false
})
.addOnSuccessListener { location: Location? ->
if (location == null)
Toast.makeText(this, "Cannot get location.", Toast.LENGTH_SHORT).show()
else {
val lat = location.latitude
val lon = location.longitude
}
}
fusedLocationClient.getCurrentLocation(Priority.PRIORITY_HIGH_ACCURACY, object : CancellationToken() {
override fun onCanceledRequested(listener: OnTokenCanceledListener) = CancellationTokenSource().token
override fun isCancellationRequested() = false
})
.addOnSuccessListener {
if (it == null)
Toast.makeText(this, "Cannot get location.", Toast.LENGTH_SHORT).show()
else {
val lat = it.latitude
val lon = it.longitude
}
}