You can get the current location and local places of user by using the Google Places API.
Ar first, you should call the [PlaceDetectionApi.getCurrentPlace()](<https://developers.google.com/android/reference/com/google/android/gms/location/places/PlaceDetectionApi#getCurrentPlace(com.google.android.gms.common.api.GoogleApiClient,%20com.google.android.gms.location.places.PlaceFilter)>)
method in order to retrieve local business or other places. This method returns a [PlaceLikelihoodBuffer](<https://developers.google.com/android/reference/com/google/android/gms/location/places/PlaceLikelihoodBuffer>)
object which contains a list of [PlaceLikelihood](<https://developers.google.com/android/reference/com/google/android/gms/location/places/PlaceLikelihood>)
objects. Then, you can get a [Place](<https://developers.google.com/android/reference/com/google/android/gms/location/places/Place>)
object by calling the [PlaceLikelihood.getPlace()](<https://developers.google.com/android/reference/com/google/android/gms/location/places/PlaceLikelihood.html#getPlace()>)
method.
Important: You must request and obtain the [ACCESS_FINE_LOCATION](<https://developer.android.com/reference/android/Manifest.permission.html#ACCESS_FINE_LOCATION>)
permission in order to allow your app to access precise location information.
private static final int PERMISSION_REQUEST_TO_ACCESS_LOCATION = 1;
private TextView txtLocation;
private GoogleApiClient googleApiClient;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
txtLocation = (TextView) this.findViewById(R.id.txtLocation);
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.enableAutoManage(this, this)
.build();
getCurrentLocation();
}
private void getCurrentLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Log.e(LOG_TAG, "Permission is not granted");
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},PERMISSION_REQUEST_TO_ACCESS_LOCATION);
return;
}
Log.i(LOG_TAG, "Permission is granted");
PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi.getCurrentPlace(googleApiClient, null);
result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
@Override
public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
Log.i(LOG_TAG, String.format("Result received : %d " , likelyPlaces.getCount() ));
StringBuilder stringBuilder = new StringBuilder();
for (PlaceLikelihood placeLikelihood : likelyPlaces) {
stringBuilder.append(String.format("Place : '%s' %n",
placeLikelihood.getPlace().getName()));
}
likelyPlaces.release();
txtLocation.setText(stringBuilder.toString());
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_TO_ACCESS_LOCATION: {
// If the request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getCurrentLocation();
} else {
// Permission denied, boo!
// Disable the functionality that depends on this permission.
}
return;
}
// Add further 'case' lines to check for other permissions this app might request.
}
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.e(LOG_TAG, "GoogleApiClient connection failed: " + connectionResult.getErrorMessage());
}