【发布时间】:2018-03-30 00:37:45
【问题描述】:
我在 Android 7.1.2 上使用 Nexus 6p。我将设备定位精度设置为HIGH_ACCURACY。
在我的代码中,我有一个位置更新的后台服务,使用FusedLocation API。如您所见,我还将位置请求优先级设置为HIGH_ACCURACY。
现在我已经在设备放在我的桌子上没有任何移动的情况下对其进行了测试,并且在大多数情况下我得到了相同的 LAT/LONG 值,有时我看到 LAT/LONG 值的微小变化,即使设备是不动。
所以我使用getAccuracy 方法打印了位置精度。
文档说,如果 getAccuracy 方法返回值 >= 68,则意味着设备(用户)很有可能位于此位置。
所以我尝试使用这种方法(下面代码的另一个版本)并检查 getAccuracy 是否返回值 >= 68,然后打印位置详细信息。
你猜怎么了?它没有太大帮助..
接下来我尝试旋转设备,我看到当设备处于背对我的横向模式时,纬度/经度位置发生了变化(即使我没有去任何地方)和准确性增加了,在某些情况下达到了 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();
}
}
【问题讨论】:
标签: android android-gps android-fusedlocation