getLastKnownLocation retourneert null

Ik heb hier wat vragen over gelezen, maar ik heb niet helemaal het antwoord gevonden dat ik nodig had.

Dus het geval is dat ik mijn kaart heb ingesteld en mijn huidige gps-locatie wil pakken.
Ik heb gecontroleerd of mijn variabelen niet NULL zijn, maar toch mijn resultaat van:

getLastKnownLocation(provider, false);

Geeft me echter null, dus hier heb ik hulp nodig.
Ik heb machtigingen toegevoegd voor de locatie GROF + FIJN. Maar meestal heb ik allerlei netwerkgegevens uitgeschakeld voor mijn telefoon, omdat ik niet erg blij ben met onvoorspelbare datastroomkosten op mijn telefoonrekening. Ik heb dus alleen wifi ingeschakeld en verbonden voor deze test.

Is er nog iets dat IK MOET inschakelen om dit mogelijk te maken? Ik zou denken dat wifi voldoende zou moeten zijn?


Antwoord 1, autoriteit 100%

Gebruik deze methode om de laatst bekende locatie te krijgen:

LocationManager mLocationManager;
Location myLocation = getLastKnownLocation();
private Location getLastKnownLocation() {
    mLocationManager = (LocationManager)getApplicationContext().getSystemService(LOCATION_SERVICE);
    List<String> providers = mLocationManager.getProviders(true);
    Location bestLocation = null;
    for (String provider : providers) {
        Location l = mLocationManager.getLastKnownLocation(provider);
        if (l == null) {
            continue;
        }
        if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
            // Found best last known location: %s", l);
            bestLocation = l;
        }
    }
    return bestLocation;
}

Antwoord 2, autoriteit 16%

je mixt de nieuwe en de oude locatie-API’s met elkaar terwijl je dat niet zou moeten doen.

om de laatst bekende locatie te krijgen, hoef je alleen maar te bellen

compile "com.google.android.gms:play-services-location:11.0.1"
mLocationClient.getLastLocation();

zodra de locatieservice was verbonden.

lees hoe u de nieuwe locatie-API gebruikt

http://developer.android.com/training/location/ retrieve-current.html#GetLocation

private void getLastLocationNewMethod(){
    FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
    mFusedLocationClient.getLastLocation()
            .addOnSuccessListener(new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {
                    // GPS location can be null if GPS is switched off
                    if (location != null) {
                        getAddress(location);
                    }
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.d("MapDemoActivity", "Error trying to get last GPS location");
                    e.printStackTrace();
                }
            });
}

Antwoord 3, autoriteit 4%

U probeert de locatie in de cache op te halen van de netwerkprovider. U moet een paar minuten wachten tot u een geldige fix krijgt. Aangezien de cache van de netwerkprovider leeg is, krijgt u daar duidelijk een nul.


Antwoord 4

Vervang je regel door

Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);

Other episodes