Current Place

欧州経済領域(EEA)のデベロッパー

Places SDK for Android を使用すると、デバイスで現在報告されている位置にある場所を探すことができます。場所の例としては、地域のお店やサービス、スポット、地理的位置などがあります。

権限

ライブラリを使用するために、アプリのマニフェストで追加の権限を宣言する必要はありません。ライブラリでは、マニフェストで使用されるすべての権限が宣言されます。ただし、アプリで PlacesClient.findCurrentPlace() を使用する場合は、実行時に位置情報の利用許可をリクエストする必要があります。

アプリで PlacesClient.findCurrentPlace() を使用しない場合は、ライブラリによって導入された ACCESS_FINE_LOCATION 権限と ACCESS_COARSE_LOCATION 権限を明示的に削除します。そのためには、マニフェストに次のコードを追加します。

<manifest ... xmlns:tools="http://schemas.android.com/tools">
    ...
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" tools:node="remove"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" tools:node="remove"/>
    ...
</manifest>

権限について詳しくは、 EasyPermissions の使用をご検討ください。

現在の場所を取得する

デバイスの現在地にあるお店やサービスなどのスポットを特定する手順は次のとおりです。

  1. ContextCompat.checkSelfPermission を呼び出して、ユーザーがデバイスの位置情報へのアクセス権を付与しているかどうかを確認します。アプリには、ユーザーに権限を求めるプロンプトを表示し、結果を処理するコードも含まれている必要があります。詳しくは、アプリの権限をリクエストするをご覧ください。
  2. FindCurrentPlaceRequest を作成し、Place.FieldList を渡して、アプリがリクエストする場所データのタイプを指定します。
  3. PlacesClient.findCurrentPlace() を呼び出し、前の手順で作成した FindCurrentPlaceRequest を渡します。
  4. FindCurrentPlaceResponse から PlaceLikelihood のリストを取得します。

フィールドは Place Search の結果に対応しており、Basic(基本)、Contact(連絡先)、Atmosphere(雰囲気)の 3 つの請求カテゴリに分けられます。Basic フィールドは基本レートで課金され、追加料金はかかりません。Contact フィールドと Atmosphere フィールドはより高いレートで課金されます。場所のデータのリクエストに対する課金方法については、使用量と課金をご覧ください。

API は TaskFindCurrentPlaceResponse を返します。FindCurrentPlaceResponse には、デバイスが設置されている可能性のある場所を表す PlaceLikelihood オブジェクトのリストが含まれています。場所ごとに、その場所が正しい可能性を示す結果が返されます。指定されたデバイスの位置情報に対応する既知の場所がない場合、リストは空になることがあります。

PlaceLikelihood.getPlace() を呼び出して Place オブジェクトを取得し、PlaceLikelihood.getLikelihood() を呼び出して場所の可能性の評価を取得できます。値が大きいほど、その場所が最適な一致である可能性が高くなります。

次のコードサンプルは、デバイスが最も可能性の高い場所のリストを取得し、各場所の名前と可能性をログに記録します。

Kotlin

// Use fields to define the data types to return.
val placeFields = listOf(Place.Field.DISPLAY_NAME, Place.Field.FORMATTED_ADDRESS, Place.Field.LOCATION)
val request = FindCurrentPlaceRequest.newInstance(placeFields)

// Call findCurrentPlace and handle the response (first check that the user has granted permission).
if (ContextCompat.checkSelfPermission(this, permission.ACCESS_FINE_LOCATION) ==
    PackageManager.PERMISSION_GRANTED
) {
    placesClient.findCurrentPlace(request).addOnSuccessListener { response ->
        binding.progressBar.visibility = View.GONE
        adapter.setPlaceLikelihoods(response.placeLikelihoods)
    }.addOnFailureListener { exception ->
        binding.progressBar.visibility = View.GONE
        if (exception is ApiException) {
            Log.e(TAG, "Place not found: ${exception.statusCode}")
        }
    }
} else {
    // A local method to request required permissions;
    // See https://developer.android.com/training/permissions/requesting
    getLocationPermission()
}

      

Java

// Use fields to define the data types to return.
List<Place.Field> placeFields = Arrays.asList(Place.Field.DISPLAY_NAME, Place.Field.FORMATTED_ADDRESS, Place.Field.LOCATION);

// Use the builder to create a FindCurrentPlaceRequest.
FindCurrentPlaceRequest request = FindCurrentPlaceRequest.newInstance(placeFields);

// Call findCurrentPlace and handle the response (first check that the user has granted permission).
if (ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
    Task<FindCurrentPlaceResponse> placeResponse = placesClient.findCurrentPlace(request);
    placeResponse.addOnCompleteListener(task -> {
        binding.progressBar.setVisibility(View.GONE);
        if (task.isSuccessful()) {
            FindCurrentPlaceResponse response = task.getResult();
            adapter.setPlaceLikelihoods(response.getPlaceLikelihoods());
        } else {
            Exception exception = task.getException();
            if (exception instanceof ApiException apiException) {
                Log.e("CurrentPlace", "Place not found: " + apiException.getStatusCode());
            }
        }
    });
} else {
    // A local method to request required permissions;
    // See https://developer.android.com/training/permissions/requesting
    getLocationPermission();
}

      

Likelihood 値について:

  • 可能性は、単一のリクエストで返された場所のリスト内で、その場所が最も一致する可能性の相対的な確率を示します。異なるリクエスト間で可能性を比較することはできません。
  • 尤度の値は 0.0 ~ 1.0 の範囲になります。

たとえば、正しい場所が場所 A である可能性が 55%、場所 B である可能性が 35% であることを表す場合、レスポンスには 2 つのメンバー(場所 A の可能性が 0.55、場所 B の可能性が 0.35)が含まれます。

アプリに属性を表示する

アプリが PlacesClient.findCurrentPlace() から取得した情報を表示する場合、アプリは帰属情報も表示する必要があります。アトリビューションに関するドキュメントをご覧ください。