Ich versuche, den Standort des Benutzers mit dem GoogleApiClient zu bekommen, es funktioniert gut, wenn das GPS aktiviert ist, wenn die Aktivität startet, wenn es jedoch nicht aktiviert ist und ich den Benutzer auffordern, das zu aktivieren GPS, ich kann den Ort nicht finden. Ich versuche, einen LocationListener zu verwenden. Ich habe sowohl die nativen als auch die GooglePlayServices-Listener ausprobiert. Hier ist mein Code:Ort kann nicht gefunden werden googleApiClient
Überprüfen Sie, ob GPS in onCreate aktiviert ist:
//Ask the user to enable GPS if it is not.
mLocationRequestBalancedPowerAccuracy = new LocationRequest()
.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
.setInterval(3600000)
.setFastestInterval(300000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequestBalancedPowerAccuracy);
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(@NonNull LocationSettingsResult result) {
final Status status = result.getStatus();
//final LocationSettingsStates states = result.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
status.startResolutionForResult(
MainActivity.this,
REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
Log.d("Location error", e.toString());
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
break;
}
}
});
Wenn das Ergebnis Auflösung erforderlich ist, ich die onResult Methode laufen, wo ich hören für Standort starten, wenn der Benutzer GPS zu ermöglichen, akzeptiert:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
switch (requestCode) {
case REQUEST_CHECK_SETTINGS:
switch (resultCode) {
case Activity.RESULT_OK:
// All required changes were successfully made
// User accepted to use GPS, start searching for location
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequestBalancedPowerAccuracy, this);
Log.d("locationmanager", "location result ok");
break;
case Activity.RESULT_CANCELED:
// The user was asked to change settings, but chose not to
this.finishAffinity();
break;
default:
break;
}
break;
}
}
Die Zeile Log.d ("locationmanager", "Lokationsergebnis ok"); wird ausgeführt, aber die obige Zeile scheint nicht ausgeführt zu werden.
Schließlich ist hier meine Zuhörer:
@Override
public void onLocationChanged(Location location) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
Log.d("changed location", String.valueOf(location.getLatitude()) + " " + String.valueOf(location.getLongitude()));
}
Stellen Sie eine Standortanfrage in der onConnected() - Funktion. – Shuddh
Ich mache das schon, aber wenn der Ort beim Start nicht verfügbar ist, ist der Ort in onConnected null. –