Places SDK for Android (Legacy) supports Current Place (Legacy). If you are familiar with the Current Place (Legacy), Nearby Search (New) makes the following changes:
Uses a new pricing model. For pricing information for all APIs, see Places SDK for Android (New).
You must initialize your app by calling the
Places.initializeWithNewPlacesApiEnabled()
method. For more information on selecting the Places API service, see Set up your Google Cloud project.Field masking is required. You must specify which fields you want returned in the response. There is no default list of returned fields. If you omit this list, the methods return an error.
Nearby Search (New) does not support
PlaceLikelihood
. With Nearby Search (New), you can use the result order to determine the most likely location.
Nearby Search (New) examples
For more information, as well as examples of how to use Nearby Search (New), see the Nearby Search (New) documentation.
Use Nearby Search (New) to get the current place
The following sample demonstrates how to get the current place with Nearby
Search (New) by replacing the use of PlacesClient.findCurrentPlace()
with
PlacesClient.searchNearby()
:
public class MainActivity extends extends AppCompatActivity {
private FusedLocationProviderClient fusedLocationProviderClient;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
// ...
// get permission
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
// get location and search
fusedLocationProviderClient
.getLastLocation()
.addOnSuccessListener(
this,
location -> {
if (location != null) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
CircularBounds circle = CircularBounds.newInstance(latLng, 10);
List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.DISPLAY_NAME);
SearchNearbyRequest.Builder request = SearchNearbyRequest.builder(circle, placeFields);
placesClient
.searchNearby(request.build())
.addOnSuccessListener(response ->
List<Place> places = response.getPlaces();)
.addOnFailureListener(
exception -> {
// handle failure
});
} else {
// failed to get location.
}
})
.addOnFailureListener(
e -> {
// handle error
});
} else {
ActivityCompat.requestPermissions(
this,
new String[] {Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSION_REQUEST_CODE);
}
}
}