Tombol lokasi Android adalah elemen UI sistem yang dapat disesuaikan dan dirancang untuk menyederhanakan cara Anda meminta akses lokasi akurat cakupan sesi. Dengan memulai permintaan lokasi melalui tindakan pengguna langsung, tombol ini meningkatkan privasi pengguna dan mengurangi kesulitan dialog izin berulang yang biasanya ditemui dengan pemberian izin sementara "Hanya kali ini".
Jika aplikasi Anda menargetkan Android 17 (level API 37) atau yang lebih baru dan hanya berisi fitur yang memerlukan akses lokasi berbasis sesi agar berfungsi, kebijakan Google Play mengharuskan Anda menggunakan tombol lokasi. Untuk mengetahui detail selengkapnya, lihat kebijakan tombol lokasi.
Kapan harus menggunakan tombol lokasi
Gunakan tombol lokasi untuk fitur yang memerlukan akses lokasi akurat berbasis sesi sesaat. Tombol ini ideal untuk aplikasi yang tidak memerlukan akses lokasi persisten dan bertujuan untuk mengurangi perintah izin "Hanya kali ini" yang berulang.
Kasus penggunaan umum mencakup:
- Fungsi "Telusuri di sekitar saya": Menemukan hotel, toko, atau restoran terdekat.
- Berbagi lokasi: Membagikan lokasi Anda saat ini satu kali kepada teman atau keluarga.
- Media sosial: Check-in atau pemberian tag lokasi.
- E-commerce: Mengisi otomatis alamat untuk pengiriman.
Menyesuaikan UI
Untuk memastikan tombol sesuai dengan estetika aplikasi Anda sekaligus tetap dapat dikenali, Anda dapat mengubah elemen visual berikut:
- Skema warna latar belakang dan ikon.
- Gaya, ukuran, dan bentuk garis luar.
- Label teks dari daftar yang telah ditentukan (misalnya, "Gunakan lokasi akurat", "Bagikan lokasi akurat").
Mengimplementasikan tombol lokasi
Untuk mengintegrasikan tombol lokasi, gunakan Library Jetpack. Library ini menyederhanakan penyiapan, menangani rendering aman di platform yang lebih baru, dan menyediakan penggantian untuk aplikasi yang menargetkan Android 16 dan yang lebih lama.
Langkah 1: Mendeklarasikan izin di Manifes Android
Anda harus mendeklarasikan izin lokasi standar beserta izin USE_LOCATION_BUTTON khusus yang diperlukan oleh layanan rendering jarak jauh sistem.
<?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>
Langkah 2: Mengimplementasikan composable Kotlin
Berikut adalah contoh implementasi tombol lokasi, termasuk contoh penggunaan opsi penyesuaian yang tersedia yang dapat digunakan untuk membuat UI sesuai dengan bagian aplikasi lainnya.
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) */ ) }
Langkah 3: Menangani kompatibilitas mundur
Library Jetpack secara otomatis menangani kompatibilitas mundur pada versi Android yang lebih lama. Pada perangkat yang menjalankan Android 16 atau yang lebih lama, library akan kembali ke komponen yang dirender secara lokal yang mempertahankan tata letak visual yang disesuaikan, tetapi kembali memicu perintah izin lokasi standar.
Dengan menggunakan pendekatan ini, Anda dapat memanfaatkan manfaat mengadopsi tombol lokasi tanpa mempertahankan solusi paralel untuk perangkat yang menjalankan Android 16 atau yang lebih lama.