Ich habe eine App, die einige Fotos schießen kann. Jedes Mal, wenn ich ein Foto mache, sollte ich eine Anfrage mit dem Längen- und Breitengrad des Benutzers senden, damit ich weiß, wo das Foto aufgenommen wurde.Gps Breite und Länge gibt mir keinen gültigen Wert
Also habe ich so etwas wie dies die Lage zu bekommen:
public class GoogleLocation implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private double lat;
private double lon;
private double alt;
private Context context;
LocationManager lm;
final int MY_PERMISSION_ACCESS_COURSE_LOCATION = 1;
public GoogleLocation(Context context) {
this.context = context;
}
public Map<String,String> getPosition() {
Map<String, String> params = new HashMap<String, String>();
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((Activity) context, new String[] { android.Manifest.permission.ACCESS_COARSE_LOCATION },
MY_PERMISSION_ACCESS_COURSE_LOCATION);
}
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null){
lat = location.getLatitude();
lon = location.getLongitude();
alt = location.getAltitude();
}
params.put("lat",String.valueOf(lat));
params.put("lon",String.valueOf(lon));
params.put("alt",String.valueOf(alt));
return params;
}
public void location(){
GoogleApiClient googleApiClient = null;
if (googleApiClient == null) {
googleApiClient = new GoogleApiClient.Builder(context)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
googleApiClient.connect();
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(30 * 1000);
locationRequest.setFastestInterval(5 * 1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
//**************************
builder.setAlwaysShow(true); //this is the key ingredient
//**************************
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
final LocationSettingsStates state = result.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied. The client can initialize location
// requests here.
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the user
// a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult(
(Activity) context, 1000);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
break;
}
}
});
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}
Aber ich habe immer die Standardwerte für den lat lon bekommen, 0.0, auf meinem GPS-Emulator habe ich einige Werte für den GPS definiert , die ich vor 2 Stunden bekommen könnte, aber ich weiß nicht, warum es aufgehört hat zu arbeiten.
Wie kann ich sie bekommen, was mache ich hier falsch?
Warum dann der Emulator ein GPS-Simulator hat? wenn es nicht wie ausgeführt funktioniert? seltsam. –
Ich habe die Änderungen gemacht, die Sie erwähnt haben und immer noch das gleiche Ergebnis erhalten –