/ / Android-Standort: Erste Position abrufen - Android, GPS, Standortmanager

Android Standort: Holen Sie sich den ersten Standort - Android, GPS, Locationmanager

Ich muss ein paar Sachen berechnen, wenn ich den ersten Standort bekomme (es muss nicht sehr genau sein).

Ich habe einen LocationManager und einen LocationListenerund bis jetzt habe ich eine boolesche firstTime, die anfangs wahr ist und der onlocationChanged zum ersten Mal aufgerufen wird. Bei jedem Aufruf der onlocationChanged-Methode (alle 5 Sekunden) prüfe ich, ob firstTime falsch ist.

Diese Lösung befriedigt mich nicht wirklich, gibt es eine bessere Möglichkeit, auf erstmalige Ereignisse zu prüfen?

Ich werde Code zum besseren Verständnis bereitstellen:

boolean firstLocation = true;
LocationManager locManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);

LocationListener locListener = new LocationListener() {

@Override
public void onLocationChanged(Location location) {
if (firstLocation == true) {
sights = getListForLocation(location, GPS_INITIAL_PERIMETER);
firstLocation = false;
}
// do something everytime onLocationChanged is called

}
};

/**
* starts the listener for GPS-Positions
*/
public void start() {
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
GPS_UPDATE_TIME, GPS_UPDATE_DISTANCE, locListener);
}

Wie Sie sehen, muss ich nur etwas tun, wenn ich zum ersten Mal einen neuen Standort erhalte.

Antworten:

1 für die Antwort № 1

Initiere deinen Standort mit null und überprüfe es:

if(lastKnownLocation == null)
{
//first time
}

und in LocationListener:

@Override
public void onLocationChanged(Location location)
{
lastKnownLocation = location;
}