问题
I am getting current Lats and Longs using FusedLocationProviderClient
and I am using the below code to get current lats and longs of the device:
public class FusedGpsService extends Service {
private FusedLocationProviderClient fusedLocationProviderClient;
private double speed;
Data datas;
List<Location> locationList;
@Override
public void onCreate() {
super.onCreate();
fusedLocationProviderClient= LocationServices.getFusedLocationProviderClient(this);
requestLocations();
Log.i("STATR","Started");
}
@SuppressLint("MissingPermission")
private void requestLocations() {
LocationRequest locationRequest=new LocationRequest();
locationRequest.setInterval(1000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
fusedLocationProviderClient.requestLocationUpdates(locationRequest,locationCallback, Looper.myLooper());
}
LocationCallback locationCallback=new LocationCallback(){
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
datas=MainActivity.getData();
locationList = locationResult.getLocations();
if (locationList.size()>0){
Location location =locationList.get(locationList.size() -1);
double lat = location.getLatitude();
double lon = location.getLongitude();
Log.i("LATLONG", "Lat: "+String.valueOf(lat)+","+"Long: "+String.valueOf(lon));
//speed = (double) ((location.getSpeed() * 3600)/1000);
//datas.setCurrSpeed(speed);
datas.update();
//Log.i("SPIIID", String.valueOf(speed)+ "km/h");
}
}
};
@Override
public void onDestroy() {
super.onDestroy();
if (fusedLocationProviderClient!=null){
fusedLocationProviderClient.removeLocationUpdates(locationCallback);
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
Everything seems to be good but if I stand still and I'm not moving the onLocationChanged()
method still gets called and keep updating it into different Lats and Longs.
Are there any solution for this.
回答1:
FusedLocationProviderClient does not give accurate value, use Location Manager and check accuracy of location by location.getAccuracy() and use location with accuracy less than 20.
来源:https://stackoverflow.com/questions/51682688/location-keep-changing-even-if-the-device-is-not-moving