我正在使用Nexus 6p和Android 7.1.2。我将设备位置精确度设置为HIGH_ACCURACY
。 在我的代码中,我有一个使用FusedLocation
API的位置更新后台服务。如您所见,我还将位置请求优先级设置为HIGH_ACCURACY
。 现在我已经测试了它,当设备在我的桌面上时没有任何移动,并且大部分时间我都有相同的LAT/LONG值,有时我看到LAT/LONG值有轻微变化,即使设备没有动。 因此,我使用getAccuracy
方法打印了位置精度。 该文档说,如果getAccuracy
方法返回值> = 68,这意味着设备(用户)位于此位置的可能性很高。 所以我尝试使用这种方法(下面的代码的另一个版本),并检查getAccuracy返回值> = 68,然后打印位置的详细信息。 猜猜看是什么?它并没有太多的帮助..错误位置纬度/经度
我试着下一件事是旋转设备,我看到,当设备在landsacpe模式与回到我的位置拉特/长变(即使我没有去任何地方),准确度明显增加,在某些情况下达到的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();
}
}
谢谢我会试试 – Elior
我也为你编辑答案。 –
好吧..它似乎工作.. 所以,如果我得到它的权利.. setSmallestDisplacement只会导致位置更新,如果有差异27米(在你的例子中) – Elior