8

我使用Fused位置Api,我以約10米的精度獲取位置。有些時候它可以提供4到8米的精度。我想要使​​用此融合位置Api或其他任何方式獲得更高的準確性。有沒有辦法以小於4米的精度獲得位置?我想獲得谷歌融合的位置API的準確位置?

我以這種方式獲取位置。

public class GetCurrentLocation implements 
      ConnectionCallbacks, OnConnectionFailedListener, LocationListener { 

     private static final String TAG = "location-updates-sample"; 
     public static final long UPDATE_INTERVAL_IN_MILLISECONDS = 0; 
     public static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = 
       UPDATE_INTERVAL_IN_MILLISECONDS/2; 
     private final String REQUESTING_LOCATION_UPDATES_KEY = "requesting-location-updates-key"; 
     private final String LOCATION_KEY = "location-key"; 
     private final String LAST_UPDATED_TIME_STRING_KEY = "last-updated-time-string-key"; 
     private GoogleApiClient mGoogleApiClient; 
     private LocationRequest mLocationRequest; 

     private Context mContext; 
     private getLocation mGetCurrentLocation; 

     public GetCurrentLocation(Context context) { 
      mContext = context; 

      buildGoogleApiClient(); 
     } 

     private synchronized void buildGoogleApiClient() { 
      Log.i(TAG, "Building GoogleApiClient"); 
      mGoogleApiClient = new GoogleApiClient.Builder(mContext) 
        .addConnectionCallbacks(this) 
        .addOnConnectionFailedListener(this) 
        .addApi(LocationServices.API) 
        .build(); 
      createLocationRequest(); 
     } 

     public interface getLocation{ 
      public void onLocationChanged(Location location); 
     } 

     public void startGettingLocation(getLocation location) { 
      mGetCurrentLocation = location; 
      connect(); 
     } 

     public void stopGettingLocation() { 
      stopLocationUpdates(); 
      disconnect(); 
     } 

     private void createLocationRequest() { 
      mLocationRequest = new LocationRequest(); 
      mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS); 
      mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS); 
      mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
     } 

     private void startLocationUpdates() { 
      if (mGoogleApiClient.isConnected()) { 
       LocationServices.FusedLocationApi.requestLocationUpdates(
         mGoogleApiClient, mLocationRequest, this); 
      } 
     } 
    private void stopLocationUpdates() { 
     LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
    } 



    private void connect() { 
     mGoogleApiClient.connect(); 
    } 

    private void disconnect() { 
     if (mGoogleApiClient.isConnected()) { 
      mGoogleApiClient.disconnect(); 
     } 
    } 

    @Override 
    public void onConnected(Bundle connectionHint) { 
     Log.i(TAG, "Connected to GoogleApiClient"); 
     startLocationUpdates(); 

    } 

    @Override 
    public void onLocationChanged(Location location) { 
     mGetCurrentLocation.onLocationChanged(location); 
    } 

    @Override 
    public void onConnectionSuspended(int cause) { 
     Log.i(TAG, "Connection suspended"); 
     mGoogleApiClient.connect(); 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult result) { 
     Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode()); 
    } 
} 

而我在我的活動代碼中使用這個類。

private GetCurrentLocation mListen; 
mListen = new GetCurrentLocation(this); 
      mListen.startGettingLocation(new GetCurrentLocation.getLocation() { 
       @Override 
       public void onLocationChanged(Location location) { 
        // Here is my working with location object 
       } 

      }); 

如何優化此代碼以獲得準確的位置。 謝謝。

+0

我覺得這個代碼是知府。我不認爲它可以更優化。 – Umar 2015-02-06 13:38:22

回答

4

定位精度基於GPS /網絡提供商,您只能等待並從中獲取位置。

您可以確定準確性再次

@Override 
public void onLocationChanged(Location location) { 
    int suitableMeter = 20; // adjust your need 
    if (location.hasAccuracy() && location.getAccuracy() <= suitableMeter) { 
     // This is your most accurate location. 
    } 

} 
+1

如果location.hasAccuracy()爲false,則location.getAccuracy()將始終返回0.0。在評估位置的準確性之前,您必須檢查hasAccuracy()。請參閱http://developer.android.com/reference/android/location/Location.html#getAccuracy() – 2015-11-12 04:00:04

+0

@ MuchOverflow謝謝,我更新了答案。 – james 2016-12-05 09:53:35

相關問題