Android の位置情報ボタンは、セッション スコープの正確な位置情報へのアクセスをリクエストする方法を簡素化するために設計された、カスタマイズ可能なシステム UI 要素です。ユーザーの直接的な操作によって位置情報の取得リクエストを開始することで、このボタンはユーザーのプライバシーを保護し、一時的な「今回のみ」の許可でよく発生する、権限ダイアログの繰り返し表示による煩わしさを軽減します。
アプリが Android 17(API レベル 37)以降をターゲットとしており、セッションベースの位置情報へのアクセスを必要とする機能のみが含まれている場合、Google Play のポリシーでは位置情報ボタンを使用することが義務付けられています。詳しくは、位置情報ボタンに関するポリシーをご覧ください。
位置情報ボタンを使用する場面
正確な位置情報への一時的なセッションベースのアクセスを必要とする機能には、位置情報ボタンを使用します。これは、位置情報への永続的なアクセスを必要とせず、[今回のみ] の権限プロンプトの繰り返しを減らすことを目的とするアプリに最適です。
一般的なユースケースには次のものがあります。
- 「周辺検索」機能: 近くのホテル、店舗、レストランを検索する。
- 現在地の共有: 友だちや家族と現在地を 1 回共有します。
- ソーシャル メディア: チェックインや位置情報のタグ付け。
- e コマース: 配達先の住所を自動入力する。
UI をカスタマイズする
ボタンがアプリの美観に合致し、かつ認識可能な状態を維持できるように、次の視覚要素を変更できます。
- 背景とアイコンのカラーパターン。
- アウトラインのスタイル、サイズ、形状。
- 事前定義されたリストのテキストラベル(「正確な位置情報を使用」、「正確な位置情報を共有」など)。
位置情報ボタンを実装する
位置情報ボタンを統合するには、Jetpack ライブラリを使用します。このライブラリは、セットアップを簡素化し、新しいプラットフォームでの安全なレンダリングを処理し、Android 16 以下をターゲットとするアプリのフォールバックを提供します。
ステップ 1: Android マニフェストで権限を宣言する
システムのリモート レンダリング サービスに必要な専用の USE_LOCATION_BUTTON 権限とともに、標準の位置情報権限を宣言する必要があります。
<?xml version="1.0" encoding="utf-8"?> <!-- Copyright 2026 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <manifest xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 1. Standard Coarse and Fine Location Permissions --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- Optional: If your app is only using the location button to access location, you should add the "onlyForLocationButton" flag shown below to your ACCESS_FINE_LOCATION declaration. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:usesPermissionFlags="onlyForLocationButton"/> Note: Adding this flag restricts your app from accessing the precise location permission via the broader permission, and that users will be required to use the location button in order to share precise location with the app. This is designed to improve user privacy & trust when granting location access. --> <!-- 2. CRITICAL: Required system permission for rendering the LocationButton --> <uses-permission android:name="android.permission.USE_LOCATION_BUTTON" /> <application android:icon="@mipmap/ic_launcher" android:label="LocationButtonSample" android:theme="@style/Theme.PinPoint"> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
ステップ 2: Kotlin コンポーザブルを実装する
以下は、位置情報ボタンの実装例です。利用可能なカスタマイズ オプションを使用して、アプリの他の部分と UI を一致させる例も含まれています。
import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.width import androidx.compose.runtime.Composable import androidx.compose.ui.graphics.Color import androidx.compose.ui.unit.dp import androidx.core.locationbutton.compose.LocationButton import androidx.core.locationbutton.compose.LocationButtonTextType @Composable fun LocationPermissionScreen(onPermissionGranted: () -> Unit, onPermissionDenied: () -> Unit) { // Renders the secure system-trusted Location Button composable LocationButton( // Callback triggered when the user taps the secure button and makes a decision on the permission dialog onPermissionResult = { isGranted -> if (isGranted) { onPermissionGranted() } else { onPermissionDenied() } }, /* ============================================================================ * VISUAL CUSTOMIZATIONS * Un-comment any of the parameters below to customize the button's aesthetics. * If omitted, the button falls back to secure, high-contrast system defaults. * ============================================================================ */ /* // LABEL TEXT TYPE: // Predefined system strings rendered inside the secure process. // Options: PreciseLocation, UsePreciseLocation, SharePreciseLocation, // NearMyPreciseLocation, or None (for an icon-only button). textType = LocationButtonTextType.UsePreciseLocation, // COLOR PALETTE: // Customize the container background, text label, and icon tint colors. backgroundColor = Color(0xFF00796B), // e.g., Material Teal textColor = Color.White, iconTint = Color(0xFFFFC107), // e.g., Amber icon tint // CORNER RADIUS & SHAPE: // Define the resting corner radius and the morphed radius when pressed. cornerRadius = 24.dp, // Rounded capsule shape pressedCornerRadius = 12.dp, // Morphs to sharper corners on tap // OUTLINE STROKE (BORDERS): // Add a contrasting outline stroke around the button bounds. strokeColor = Color(0xFF004D40), strokeWidth = 2.dp, // INTERACTIVE TOUCH PADDING: // Defines the secure clickable touch target boundary. // Coerced securely by the system between 4.dp and 8.dp. clickablePadding = PaddingValues(6.dp) */ ) }
ステップ 3: 下位互換性を処理する
Jetpack ライブラリは、以前のバージョンの Android での下位互換性を自動的に処理します。Android 16 以前を搭載しているデバイスでは、ライブラリはローカルでレンダリングされたコンポーネントにフォールバックします。このコンポーネントは、カスタマイズされたビジュアル レイアウトを保持しますが、標準の位置情報の利用許可プロンプトをトリガーするように戻ります。
このアプローチを使用すると、Android 16 以前を搭載したデバイス向けの並行ソリューションを維持することなく、位置情報ボタンを採用するメリットを活用できます。