javaandroidlocationgoogle-places-apigoogle-places

Can Google's Places API findCurrentPlace method be used to get the exact current address, rather than the address of the closest 'Place'?


I am currently using the Google Places API within my (Java) Android application to get the current device location and display it into an editText. Currently when I make the call to placesClient.findCurrentPlace(request), a list of the closest places and the likelihood of the device being there are returned, like so:

Place 'XXXXXXX' has likelihood: 0.600000
Place 'YYYYYYY' has likelihood: 0.0500000
Place 'ZZZZZZZ' has likelihood: 0.00000

Where the places are things like "Thames River" or "Telus Stadium". I can then get the address from each of these from the response. My question is, is there any way to get back the address of the phones current location, opposed to the address of the closest 'Place'?

I have done something similar with the Places Autocomplete so that it suggests addresses rather than places, which works very well. Getting the current place is done in a slightly different way though, and therefore I can't seem to make the same change.

The reason that I am attempting to do this using the Places API is because I have a start and end location AutoCompleteEditText which the user can type into. They can choose to have their current location shown in the Start Location AutoCompleteEditText, and I was hoping to show the address in the same format as it would be in if they had typed in the address and clicked it using the Places AutoComplete.

Here is the code currently being used to get the address of the closest place, which I would like to modify to get the address the phone is currently at.

List<Place.Field> placeFields = Collections.singletonList(Place.Field.ADDRESS);
FindCurrentPlaceRequest request = FindCurrentPlaceRequest.newInstance(placeFields);
...
Task<FindCurrentPlaceResponse> placeResponse = placesClient.findCurrentPlace(request);
placeResponse.addOnCompleteListener(new OnCompleteListener<FindCurrentPlaceResponse>() {
    @Override
    public void onComplete(@NonNull Task<FindCurrentPlaceResponse> task) {
        if (task.isSuccessful()) { 
            FindCurrentPlaceResponse response = task.getResult();
            for (PlaceLikelihood placeLikelihood : response.getPlaceLikelihoods()) {
                Log.i(LOG_TAG, String.format("Place '%s' has likelihood: %f",
                placeLikelihood.getPlace().getAddress(),
                placeLikelihood.getLikelihood()));
            }
        } else {
            Log.e(LOG_TAG, "Error finding current location");
        }
    }
});

Solution

  • Forget having to use the Google Places API for this - It's simple to do without having to interact with any API.

    Simply get the current location using a FusedLocationProviderClient to return the last known latitude and longitude of the device (as seen here), and then use an instance of Geocoder to get addresses from that latitude and longitude using the getFromLocation method. This can then easily be converted into a string which matches what the Google Places API would have returned (as seen here).