简体   繁体   中英

(Android) How to retrieve current location using Google Play Services

I'm fairly new to Android app development and I'm currently looking into retrieving the user's current location (coordinates & city) using Google Play Services . As of now, I fail to find any clear step-by-step tutorial on doing so. Any advise or guidance would be greatly appreciated!

You can use the fused location provider APIs in the Google Play Service library to retrieve the device's last known location :

 Location lastLocation = LocationServices.FusedLocationApi
                .getLastLocation(mGoogleApiClient);

    if (lastLocation != null) {
        double latitude = lastLocation.getLatitude();
        double longitude = lastLocation.getLongitude();

//Once you get the coordinates, you can retrieve the city name using the Geocoder class:

        Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
        List<Address> addresses;
            addresses = gcd.getFromLocation(latitude, longitude, 1);
            if (addresses != null) {
                if (addresses.size() > 0)
                    String cityName = addresses.get(0).getLocality();

            }

         }

In your manifest:

  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Here's a detailed tutorial to help you

Current Location = Last location

This is what Google Android documentation says about getting current location using FusedLocationProvider

In most cases, you are interested in the user's current location, which is usually equivalent to the last known location of the device.

How to do this?

You can use this method

public void getCurrentLocation() {

      FusedLocationProviderClient locationProviderClient = LocationServices.
                                           getFusedLocationProviderClient(this);


     locationProviderClient.getLastLocation()
            .addOnSuccessListener(new OnSuccessListener<Location>() {

                  @Override
                  public void onSuccess(Location location) {

                     Log.d("Location",currentLocation.toString());

                  }
             });

      }

}

You need to add <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
The above code is the simplified version. If your app runs on the Android Lollipop and above then you need to ask for Location permission at runtime ALSO .

You can see the full code HERE .

You do not need to connect GoogleApiClient in latest google play services, you can directly request location updates.

mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

        locationRequest = LocationRequest.create();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(20 * 1000);
mFusedLocationProviderClient.requestLocationUpdates(locationRequest,locationCallback, Looper.myLooper());
        locationCallback = new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult locationResult) {

            }
        };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM