6

Ich versuche, einen Dienst zu erstellen, der alle x Sekunden und nach y Entfernung Aktualisierungen des Standorts erhält. Ich erhalte Updates nach x sec, aber nie nach y Entfernung. Ich habe es mehrfach mit verschiedenen Werten getestet und es scheint, dass setSmallestDisplacement überhaupt nicht funktioniert. Es gab verschiedene Beiträge zu dieser Frage, aber ohne irgendeine Lösung. Ich würde es schätzen, wenn jemand mir helfen oder sogar in eine andere Richtung weisen könnte.Fused Location Api setSmallestDisplacement ignoriert/funktioniert nicht

Mein Service

public class GPS_Service extends Service implements GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener, LocationListener { 

private GoogleApiClient mGoogleApiClient; 
private LocationRequest mLocationRequest; 
private int timethresh; 
private int distancethresh; 
protected Location mCurrentLocation; 

@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

@Override 
public void onCreate() { 
    super.onCreate(); 

    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addApi(LocationServices.API) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .build(); 
    mGoogleApiClient.connect(); 

} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 

    mLocationRequest = new LocationRequest(); 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    //Set the desired interval for active location updates, in milliseconds. 
    mLocationRequest.setInterval(60* 1000); 
    //Explicitly set the fastest interval for location updates, in milliseconds. 
    mLocationRequest.setFastestInterval(30* 1000); 
    //Set the minimum displacement between location updates in meters 
    mLocationRequest.setSmallestDisplacement(1); // float 


    return super.onStartCommand(intent, flags, startId); 

} 

@Override 
public void onConnected(Bundle bundle) { 
    if (mCurrentLocation == null) { 
     mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 

    } 
    //Requests location updates from the FusedLocationApi.  
    startLocationUpdates(); 

} 

@Override 
public void onLocationChanged(Location location) { 
    mCurrentLocation = location; 
    //Toast.makeText(this, ""+mCurrentLocation.getLatitude()+","+mCurrentLocation.getLongitude(), Toast.LENGTH_SHORT).show(); 
    System.out.println(""+mCurrentLocation.getLatitude()+","+mCurrentLocation.getLongitude()); 


} 

@Override 
public void onConnectionSuspended(int i) { 
    // The connection to Google Play services was lost for some reason. We call connect() to 
    // attempt to re-establish the connection. 
    mGoogleApiClient.connect(); 
} 


@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 
    Toast.makeText(getApplicationContext(),"Location services connection failed with code " + connectionResult.getErrorCode(), Toast.LENGTH_LONG).show(); 

} 

protected void startLocationUpdates() { 
    LocationServices.FusedLocationApi.requestLocationUpdates(
      mGoogleApiClient, mLocationRequest, this); 

} 

}

Antwort

4

Nach dieser SO question insbesondere die Antwort von CGR.

Die Verschiebung, die für LocationRequest festgelegt wurde, hat keine Möglichkeit, die Positionen zu erhalten, wenn das Gerät stillsteht, da Displacement Vorrang vor Intevals (Intervall und schnellsterIntervall) hat. Wie ich es erraten konnte - Vielleicht haben Sie ein anderes LocationRequest-Objekt (ohne Verschiebungssatz) an LocationServices.FusedLocationApi.requestLocationUpdates() übergeben.

Die Location setSmallestDisplacement ( smallestDisplacementMeters float)

die minimale Verschiebung zwischen Standortaktualisierung in m festgelegt ist. Standardmäßig ist der Wert 0. Es wird das gleiche Objekt zurückgegeben, so dass die Setter verkettet werden können.

HINWEIS: Location-Anforderungen von Anwendungen mit ACCESS_COARSE_LOCATION und nicht ACCESS_FINE_LOCATION werden automatisch in ein langsamen Intervall gedrosselt werden, und die Lage Objekt wird nur eine grobe Maß an Genauigkeit zeigt verschleiert werden.

Überprüfen Sie diese page für weitere Informationen.

2

versuchen, etwas wie folgt aus:

protected void createLocationRequest() { 
    mLocationRequest = new LocationRequest(); 

    mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS); 

    mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS); 

    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 

    mLocationRequest.setSmallestDisplacement(100); //100 meters 
}