我正在使用Nexus 6p和Android 7.1.2。我将设备位置精度设置为HIGH_ACCURACY
。
在我的代码中,我使用FusedLocation
API为位置更新提供了后台服务。如您所见,我还将位置请求优先级设置为HIGH_ACCURACY
。
现在我已经测试了它,当设备在我的桌面上没有任何动作时,大多数时候我都有相同的LAT / LONG值,有时我看到LAT / LONG值的微小变化,即使设备不是不动了。
所以我使用getAccuracy
方法打印了位置精度。
文档说如果getAccuracy
方法返回值> = 68,那么这意味着设备(用户)很可能在此位置。
所以我尝试使用此方法(下面代码的另一个版本)并检查getAccuracy是否返回值> = 68然后打印位置详细信息。
你猜怎么着?它没那么帮助..
我接下来尝试的是旋转设备,我看到当设备处于landsacpe模式时背对着我,位置lat / long被改变了(即使我没有去任何地方)和准确性增加了,在某些情况下达到了180.
所以我不明白这一点:
为什么有时设备的纬度/经度值会有所不同,即使设备躺在桌面上并且不移动?
轮换如何影响定位精度?!
我的代码:
public class LocationUpdateService extends Service implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener
{
private GoogleApiClient googleApiClient;
private LocationRequest locationRequest;
private boolean playsServiceAvailable;
private String TAG = getClass().getSimpleName();
@Override
public void onCreate()
{
Log.d(TAG,"onCreate");
super.onCreate();
initRequestLocation();
playsServiceAvailable = checkPlayServicesConnection();
setupLocationApiClient();
}
@Nullable
@Override
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.d(TAG, "onStartCommand");
super.onStartCommand(intent,flags,startId);
if (!playsServiceAvailable || googleApiClient.isConnected())
{
return START_STICKY;
}
setupLocationApiClient();
if (!googleApiClient.isConnected() || !googleApiClient.isConnecting())
{
Log.d(TAG, "onStartCommand: googleApiclient.connect()");
googleApiClient.connect();
}
return START_STICKY;
}
@Override
public void onDestroy()
{
Log.d(TAG, "onDestroy");
if (this.playsServiceAvailable && this.googleApiClient != null)
{
this.googleApiClient.unregisterConnectionCallbacks(this);
this.googleApiClient.unregisterConnectionFailedListener(this);
this.googleApiClient.disconnect();
this.googleApiClient = null;
}
super.onDestroy();
}
@Override
public void onConnected(@Nullable Bundle bundle)
{
try
{
LocationServices.FusedLocationApi.requestLocationUpdates(this.googleApiClient, locationRequest, this);
}
catch (SecurityException e)
{
e.printStackTrace();
}
}
@Override
public void onConnectionSuspended(int i)
{
googleApiClient = null;
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult)
{
}
@Override
public void onLocationChanged(Location currentLocation)
{
Log.d(TAG, "onLocationChanged:\n\t\t\t" +
"CurrentLocation:\n\t\t\t\t" +
"ACCURACY: " + currentLocation.getAccuracy() + "\n\t\t\t\t" +
"LAT: " + currentLocation.getLatitude() + "\n\t\t\t\t" +
"LONG: " + currentLocation.getLongitude() + "\n\t\t\t\t" +
"TIME: " + currentLocation.getTime());
}
private boolean checkPlayServicesConnection ()
{
int resultCode = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context);
if (resultCode != ConnectionResult.SUCCESS)
{
return false;
}
return true;
}
private void initRequestLocation()
{
locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(5000)
.setFastestInterval(1000);
}
private void setupLocationApiClient()
{
Log.d(TAG,"setupLocationApiClient");
if (googleApiClient == null)
initGoogleApiClient();
}
private synchronized void initGoogleApiClient()
{
Log.d(TAG,"initGoogleApiClient");
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addOnConnectionFailedListener(this)
.addConnectionCallbacks(this)
.build();
}
}
答案 0 :(得分:2)
这是GPS In-accuracy,这不是设备的问题。它试图获取确切的位置,这就是为什么它在这里移动尝试提高gps的准确性。尝试设置最小的位移和间隔,以获得更准确的结果。
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setSmallestDisplacement(27);//27 meter
mLocationRequest.setInterval(5000); // Update location every 5 seconds
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
mLocationRequest, this);