Add location manager helper and Google Play Services dependency
This commit is contained in:
@@ -51,6 +51,9 @@ dependencies {
|
|||||||
// CameraX Extensions (Effects) - Optional
|
// CameraX Extensions (Effects) - Optional
|
||||||
implementation "androidx.camera:camera-extensions:${camerax_version}"
|
implementation "androidx.camera:camera-extensions:${camerax_version}"
|
||||||
|
|
||||||
|
// Google Play Services for location
|
||||||
|
implementation 'com.google.android.gms:play-services-location:21.0.1'
|
||||||
|
|
||||||
testImplementation 'junit:junit:4.13.2'
|
testImplementation 'junit:junit:4.13.2'
|
||||||
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
|
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
|
||||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
|
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
|
||||||
|
|||||||
@@ -394,11 +394,17 @@ class CameraActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private var locationManagerHelper: LocationManagerHelper? = null
|
||||||
|
|
||||||
@SuppressLint("MissingPermission")
|
@SuppressLint("MissingPermission")
|
||||||
private fun getCurrentLocation(): Location? {
|
private fun getCurrentLocation(): Location? {
|
||||||
// 这里应该实现获取当前位置的逻辑
|
// 使用LocationManagerHelper获取位置
|
||||||
// 简化实现,返回null,实际应用中需要使用LocationManager或FusedLocationProviderClient
|
var location: Location? = null
|
||||||
return null
|
locationManagerHelper = LocationManagerHelper(this)
|
||||||
|
locationManagerHelper?.getLastKnownLocation { loc ->
|
||||||
|
location = loc
|
||||||
|
}
|
||||||
|
return location
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun openSettings() {
|
private fun openSettings() {
|
||||||
|
|||||||
90
app/src/main/java/com/example/app/LocationManagerHelper.kt
Normal file
90
app/src/main/java/com/example/app/LocationManagerHelper.kt
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
package com.example.app
|
||||||
|
|
||||||
|
import android.Manifest
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.pm.PackageManager
|
||||||
|
import android.location.Location
|
||||||
|
import android.location.LocationManager
|
||||||
|
import android.os.Looper
|
||||||
|
import androidx.core.app.ActivityCompat
|
||||||
|
import com.google.android.gms.location.*
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 位置服务管理器
|
||||||
|
* 处理位置获取、地理编码等功能
|
||||||
|
*/
|
||||||
|
class LocationManagerHelper(private val context: Context) {
|
||||||
|
|
||||||
|
private var fusedLocationClient: FusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(context)
|
||||||
|
private var locationCallback: LocationCallback? = null
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取最后一次已知位置
|
||||||
|
*/
|
||||||
|
fun getLastKnownLocation(onResult: (Location?) -> Unit) {
|
||||||
|
if (!hasLocationPermissions()) {
|
||||||
|
onResult(null)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fusedLocationClient.lastLocation
|
||||||
|
.addOnSuccessListener { location ->
|
||||||
|
onResult(location)
|
||||||
|
}
|
||||||
|
.addOnFailureListener {
|
||||||
|
onResult(null)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 实时获取位置
|
||||||
|
*/
|
||||||
|
fun requestLocationUpdates(callback: (Location?) -> Unit) {
|
||||||
|
if (!hasLocationPermissions()) {
|
||||||
|
callback(null)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val locationRequest = LocationRequest.create().apply {
|
||||||
|
interval = TimeUnit.SECONDS.toMillis(10) // 每10秒更新一次
|
||||||
|
fastestInterval = TimeUnit.SECONDS.toMillis(5) // 最快5秒更新一次
|
||||||
|
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
|
||||||
|
}
|
||||||
|
|
||||||
|
locationCallback = object : LocationCallback() {
|
||||||
|
override fun onLocationResult(locationResult: LocationResult) {
|
||||||
|
val location = locationResult.lastLocation
|
||||||
|
callback(location)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback!!, Looper.getMainLooper())
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 停止位置更新
|
||||||
|
*/
|
||||||
|
fun stopLocationUpdates() {
|
||||||
|
locationCallback?.let {
|
||||||
|
fusedLocationClient.removeLocationUpdates(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查位置权限
|
||||||
|
*/
|
||||||
|
private fun hasLocationPermissions(): Boolean {
|
||||||
|
return (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ||
|
||||||
|
ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查GPS是否开启
|
||||||
|
*/
|
||||||
|
fun isGPSEnabled(): Boolean {
|
||||||
|
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
||||||
|
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
|
||||||
|
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user