記得加入 Permission宣告: <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.deyu.sample"> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
App Gradle 依賴如下:
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'androidx.appcompat:appcompat:1.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.google.android.gms:play-services-location:17.0.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' }
主要的layout xml檔:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".LocationActivity"> <Button android:id="@+id/btn_request_location" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintBottom_toTopOf="@+id/tv_location" android:layout_marginTop="8dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp" android:text="按一下更新座標"/> <TextView android:id="@+id/tv_location" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" app:layout_constraintBottom_toBottomOf="parent" android:layout_marginTop="8dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"/> </android.support.constraint.ConstraintLayout>
主要Activity的程式碼:
package com.deyu.sample import android.Manifest import android.annotation.SuppressLint import android.content.Intent import android.content.pm.PackageManager import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat import com.google.android.gms.location.LocationCallback import com.google.android.gms.location.LocationRequest import com.google.android.gms.location.LocationResult import com.google.android.gms.location.LocationServices import kotlinx.android.synthetic.main.activity_location.* import java.util.* class LocationActivity : AppCompatActivity() { // 需要的權限 private val NeedPermissions = arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION) private val PERMISSION_REQUEST_CODE = 487 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_location) setup() } private fun setup() { if (!requestAllNeedPermissions()) return; btn_request_location.setOnClickListener { requestLocation() } } @SuppressLint("MissingPermission") fun requestLocation() { val mLocationProviderClient = LocationServices.getFusedLocationProviderClient(this); val locationRequest = LocationRequest(); locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) // 設定更新速度 locationRequest.setInterval(1000) // 設定要更新幾次座標 locationRequest.setNumUpdates(1) mLocationProviderClient.requestLocationUpdates(locationRequest, object : LocationCallback() { override fun onLocationResult(locationresult: LocationResult?) { locationresult ?: return val locationText = "你應該是在 經度:${locationresult.lastLocation.longitude} , 緯度${locationresult.lastLocation.latitude}" tv_location.setText(locationText) } }, null) } // 回傳是否已經有權限。 fun requestAllNeedPermissions(): Boolean { val permissionsList = ArrayList<String>() for (permission in NeedPermissions) if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) permissionsList.add(permission) if (permissionsList.size < 1) return true ActivityCompat.requestPermissions(this, permissionsList.toTypedArray(), PERMISSION_REQUEST_CODE) return false } override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) if (requestCode == PERMISSION_REQUEST_CODE) { setup() } } }
完成結果如下:
全站熱搜
留言列表