获取错误位置 lat/long
Getting wrong Location lat/long
我在 Android 7.1.2 中使用 Nexus 6p。我将设备定位精度设置为 HIGH_ACCURACY
。
在我的代码中,我有一个用于位置更新的后台服务,使用 FusedLocation
API。如您所见,我还将位置请求优先级设置为 HIGH_ACCURACY
。
现在,当设备放在我的桌子上没有任何移动时,我已经对其进行了测试,并且在大多数情况下,我得到了相同的 LAT/LONG 值,有时我甚至看到 LAT/LONG 值发生了微小的变化虽然设备没有移动。
所以我使用 getAccuracy
方法打印了位置精度。
文档说如果 getAccuracy
方法 returns 一个值 >= 68 那么这意味着设备(用户)很有可能在这个位置。
所以我尝试使用这种方法(下面代码的另一个版本)并检查 getAccuracy returns 值是否 >= 68 然后打印位置详细信息。
你猜怎么着?它没有太大帮助..
接下来我尝试旋转设备,我看到当设备处于横向模式且背对着我时,位置 lat/long 发生了变化(即使我没有去任何地方) 并且精度提高了,在某些情况下达到了 180 的值。
所以我不明白这个:
为什么有时即使设备躺在桌子上不动,设备的 lat/long 值也会不同?
旋转对定位精度有何影响?!
我的代码:
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();
}
}
这是 GPS 不准确,这不是设备问题。它会尝试获取确切位置,这就是为什么它会在这里和那里移动以尝试提高 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);
我在 Android 7.1.2 中使用 Nexus 6p。我将设备定位精度设置为 HIGH_ACCURACY
。
在我的代码中,我有一个用于位置更新的后台服务,使用 FusedLocation
API。如您所见,我还将位置请求优先级设置为 HIGH_ACCURACY
。
现在,当设备放在我的桌子上没有任何移动时,我已经对其进行了测试,并且在大多数情况下,我得到了相同的 LAT/LONG 值,有时我甚至看到 LAT/LONG 值发生了微小的变化虽然设备没有移动。
所以我使用 getAccuracy
方法打印了位置精度。
文档说如果 getAccuracy
方法 returns 一个值 >= 68 那么这意味着设备(用户)很有可能在这个位置。
所以我尝试使用这种方法(下面代码的另一个版本)并检查 getAccuracy returns 值是否 >= 68 然后打印位置详细信息。
你猜怎么着?它没有太大帮助..
接下来我尝试旋转设备,我看到当设备处于横向模式且背对着我时,位置 lat/long 发生了变化(即使我没有去任何地方) 并且精度提高了,在某些情况下达到了 180 的值。
所以我不明白这个:
为什么有时即使设备躺在桌子上不动,设备的 lat/long 值也会不同?
旋转对定位精度有何影响?!
我的代码:
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();
}
}
这是 GPS 不准确,这不是设备问题。它会尝试获取确切位置,这就是为什么它会在这里和那里移动以尝试提高 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);