即使设备没有移动,位置也会不断变化

时间:2018-08-04 05:20:35

标签: android geolocation location fusedlocationproviderapi android-fusedlocation

我正在使用FusedLocationProviderClient获取当前的经度和纬度,并且正在使用以下代码来获取设备的当前经度和纬度:

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;
}

}

一切似乎都不错,但是如果我站着不动而不动,onLocationChanged()方法仍然会被调用并继续将其更新为不同的纬度和经度。

对此有任何解决方案吗?

1 个答案:

答案 0 :(得分:0)

FusedLocationProviderClient没有给出准确的值,请使用位置管理器并按location.getAccuracy()检查位置的准确性,并使用精度小于20的位置。