Solution 1 :
Somethings you should know about getLastKnownLocation(...)
[From Document]
Gets the last known location from the given provider, or null if there is no last known location. The returned location may be quite old in some circumstances, so the age of the location should always be checked.
This will never activate sensors to compute a new location, and will only ever return a cached location.
Please try locationManager.requestSingleUpdate(...)
, this method will request user current location.
if(locationManager.allProviders.contains(LocationManager.GPS_PROVIDER)) {
locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, locListener)
} else if(locationManager.allProviders.contains(LocationManager.NETWORK_PROVIDER)){
locationManager.rerequestSingleUpdate(LocationManager.NETWORK_PROVIDER, locListener)
}
And this is listener.
val locListener = object: LocationListener {
override fun onLocationChanged(location: Location?) {
// do something with updated location here.
}
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onProviderEnabled(provider: String?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onProviderDisabled(provider: String?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
Problem :
In my Android Application I am targeting the device ranging from,
Minimum Android Version – 7.1 (API 25)
Target Android Version – 10.0 (API 29)
I am using the below logic to get the location Information
Permissions Using
<uses-permission android_name="android.permission.INTERNET"/>
<uses-permission android_name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android_name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android_name="android.permission.ACCESS_NETWORK_STATE"/>
Code Logic
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_NETWORK_STATE) != PackageManager.PERMISSION_GRANTED) {
return;
}
try {
gps_loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
network_loc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
} catch (Exception e) {
e.printStackTrace();
}
if (gps_loc != null) {
final_loc = gps_loc;
latitude = final_loc.getLatitude();
longitude = final_loc.getLongitude();
}
else if (network_loc != null) {
final_loc = network_loc;
latitude = final_loc.getLatitude();
longitude = final_loc.getLongitude();
}
else {
latitude = 0.0;
longitude = 0.0;
}
}
But most of the time I am not getting accurate location with API call. But If enable the below option in device level (in Phone), I am getting the most accurate location
Is there any way I can configure the above settings in application level through code. The client which I am working is not satisfied with changing the configuration manually.
I am wondering how I can achieve the same through code. Someone please help.
Comments
Comment posted by Stephan Ronald
answer is not relevant to the question, he was asking about enabling wifi scanning of Google location service through code.
Comment posted by Markus Kauppinen
This does make some sense though as when you are just relying on
Comment posted by Hello World
From the questuin, I think that what he want is precise location so I advice hime to use