Ich bin neu in Android und ich versuche, eine Android-App zu entwickeln, die den aktuellen Standort des Benutzers anzeigt. Ich benutze Genymotion. Jetzt verwende ichDen aktuellen Standort des Benutzers in Android finden
mLocation=LocationServices.FusedLocationApi.getLastLocation(mGoogleClient);
, um den letzten Standort des Geräts zu erhalten. Die Sache ist, ich wollte den aktuellen Standort bekommen, aber damit bekomme ich mlocation
als nicht null, das ist in Ordnung. In der Karte wird jedoch immer der letzte Standort des Geräts angezeigt, nicht der aktuelle Standort.
Code-Schnipsel
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "inside onconnected Location services connected");
Location mLocation=null;
mLocationRequest= new LocationRequest();
mLocationRequest.setInterval(10 * 1000) ;
mLocationRequest.setFastestInterval(1 * 1000);mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
}
mLocation=LocationServices.FusedLocationApi.getLastLocation(mGoogleClient);
if(mLocation==null)
{
Log.d("***Inside mlocation***", "***mlocation is null here");
}
if (mLocation != null) {
onLocationChanged(mLocation);
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleClient, mLocationRequest, this);
}
@Override
public void onLocationChanged(Location location) {
double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();
LatLng latLng = new LatLng(currentLatitude,currentLongitude);
MarkerOptions options = new MarkerOptions()
.position(latLng)
.title("Im here!");
mMap.addMarker(options);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
}
und in onstart()
rief ich googleapi.connect()
und ich setUpMapIfNeeded()
angemessen.
Bitte sagen Sie mir, was das Problem in diesem Code ist. Ich versuche das für 3 Tage.
In Ihrem Code nicht erwähnt GPS ein/aus. sollten Sie GPS während App forground einschalten. –
Hallo Jay, der GPS in meinem Emulator ist eingeschaltet, aber was ich von Ihrer Aussage verstanden habe, muss ich auf die Verfügbarkeit von GPS überprüfen, bin ich richtig .. ?? aber dann macht es auch keinen Unterschied, denke ich. – Navi
überprüfen Sie meine Antwort @Navi –