Places SDK for Android(以前のバージョン)は、現在地(以前のバージョン)をサポートしています。Current Place(以前のバージョン)をご利用の場合は、Nearby Search(新版)で次の変更が行われます。
新しい料金モデルを使用します。すべての API の料金については、 Places SDK for Android(新版)をご覧ください。
Places.initializeWithNewPlacesApiEnabled()
メソッドを呼び出してアプリを初期化する必要があります。Places API サービスの選択について詳しくは、Google Cloud プロジェクトを設定するをご覧ください。フィールドのマスキングが必要です。レスポンスで返すフィールドを指定する必要があります。返されるフィールドのデフォルトのリストはありません。このリストを省略すると、メソッドはエラーを返します。
Nearby Search(新規)は
PlaceLikelihood
をサポートしていません。Nearby Search(新版)では、結果の順序を使用して、最も可能性の高い場所を特定できます。
Nearby Search(新規)の例
詳細と Nearby Search(新版)の使用例については、Nearby Search(新版)のドキュメントをご覧ください。
Nearby Search(新版)を使用して現在の場所を取得する
次のサンプルは、PlacesClient.findCurrentPlace()
の使用を PlacesClient.searchNearby()
に置き換えて、Nearby Search(新版)で現在の場所を取得する方法を示しています。
public class MainActivity 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, 100);
List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.DISPLAY_NAME);
// Define a list of types to exclude. Adjust this list to suit each application.
final List<String> excludedTypes = Arrays.asList("public_bathroom", "beach");
SearchNearbyRequest request
= SearchNearbyRequest.builder(/* location restriction = */ circle, placeFields)
.setExcludedTypes(excludedTypes)
.setRankPreference(SearchNearbyRequest.RankPreference.DISTANCE)
.build();
placesClient
.searchNearby(request)
.addOnSuccessListener(
response -> {
List<Place> places = response.getPlaces();
// do more on the results
})
.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);
}
}
}