現在地

コレクションでコンテンツを整理 必要に応じて、コンテンツの保存と分類を行います。
プラットフォームを選択: Android iOS

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. アプリがリクエストする場所のデータタイプを指定して、Place.FieldList を渡して FindCurrentPlaceRequest を作成します。
  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() を呼び出してその場所の可能性評価を取得できます。値が大きいほど、その場所が一致する可能性が高くなります。

次のコードサンプルでは、デバイスが設置されている可能性の高い場所のリストを取得し、それぞれの場所の名前と可能性を記録します。

Java


// Use fields to define the data types to return.
List<Place.Field> placeFields = Collections.singletonList(Place.Field.NAME);

// 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 -> {
        if (task.isSuccessful()){
            FindCurrentPlaceResponse response = task.getResult();
            for (PlaceLikelihood placeLikelihood : response.getPlaceLikelihoods()) {
                Log.i(TAG, String.format("Place '%s' has likelihood: %f",
                    placeLikelihood.getPlace().getName(),
                    placeLikelihood.getLikelihood()));
            }
        } else {
            Exception exception = task.getException();
            if (exception instanceof ApiException) {
                ApiException apiException = (ApiException) exception;
                Log.e(TAG, "Place not found: " + apiException.getStatusCode());
            }
        }
    });
} else {
    // A local method to request required permissions;
    // See https://developer.android.com/training/permissions/requesting
    getLocationPermission();
}

      

Kotlin


// Use fields to define the data types to return.
val placeFields: List<Place.Field> = listOf(Place.Field.NAME)

// Use the builder to create a FindCurrentPlaceRequest.
val request: FindCurrentPlaceRequest = 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) {

    val placeResponse = placesClient.findCurrentPlace(request)
    placeResponse.addOnCompleteListener { task ->
        if (task.isSuccessful) {
            val response = task.result
            for (placeLikelihood: PlaceLikelihood in response?.placeLikelihoods ?: emptyList()) {
                Log.i(
                    TAG,
                    "Place '${placeLikelihood.place.name}' has likelihood: ${placeLikelihood.likelihood}"
                )
            }
        } else {
            val exception = task.exception
            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()
}

      

Likelihood 値について:

  • 可能性は、1 つのリクエストに対して返された場所のリストの中で、その場所が最良である確率を示します。異なるリクエスト間での可能性を比較することはできません。
  • 可能性の値は 0.0 ~ 1.0 の間です。

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

アプリに属性を表示する

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