How to get device location

So, I'm trying to create a weather application in practice, and I ran into the problem of trying to find a place. I read that people suggest using getLastLocation through a fusible API, the problem with this is that they don’t have a location registered on the device yet and it is null. I noticed the use of emulator time that this is rare, but I still like it when my application handles it properly. One case where you may encounter this is that they simply turned off the GPS and vice versa, or if the phone was turned on. One thing I did is that if getLastLocation returns to zero, you need to request an update, but you still have to wait for the device to register the updated location,which with the weather app all data is based on you, I am still facing the same problem. I noticed that with other applications this is not a problem, for example, sometimes I have to download Google Maps to force it to register a location. How does Google Maps force it to update location? Here is an example of my getLocation method:

public void getLocation() throws SecurityException {
    boolean gps_enabled;
    LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    if (gps_enabled) {

        Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (location != null) {
            startGetForecast(location);
        } else {
            LocationRequest request = LocationRequest.create();
            request.setNumUpdates(1);
            request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, request, this);
        }
    } 
    else {
        AlertDialogFragment alertDialogFragment = new AlertDialogFragment();
        alertDialogFragment.setErrorTexts("Location Unavailable", "Could not retrieve location information.", "Exit");
        alertDialogFragment.show(getSupportFragmentManager(), "Location Unavailable");
    }
}
+4
1

.. , ( ) OP, , , OP.

, - Google . Google Play. , Google Play Store. Google, Google Play , Fused Location.

, . , Play , .

, :

  • , .
  • Fused Location Provider Api, , Api :

    • GPS-
    • WiFi-
  • , GPS,

  • , GPS-, , , .

, -.

: , :

Android-: Big Nerd Ranch
2-
31 . 552 Google Play Services

+1

Source: https://habr.com/ru/post/1651432/


All Articles