2017-10-18 96 views
0

我正在使用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(); 
    } 
} 

回答

3

這是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); 
+0

謝謝我會試試 – Elior

+0

我也爲你編輯答案。 –

+0

好吧..它似乎工作.. 所以,如果我得到它的權利.. setSmallestDisplacement只會導致位置更新,如果有差異27米(在你的例子中) – Elior