即使设备没有移动,位置也会不断变化
Posted
技术标签:
【中文标题】即使设备没有移动,位置也会不断变化【英文标题】:Location keep changing even if the device is not moving 【发布时间】:2019-01-11 22:35:17 【问题描述】:我正在使用 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】:FusedLocationProviderClient 没有给出准确的值,使用 Location Manager 并通过 location.getAccuracy() 检查位置的准确性,并使用精度小于 20 的位置。
【讨论】:
好的我试试以上是关于即使设备没有移动,位置也会不断变化的主要内容,如果未能解决你的问题,请参考以下文章