添加位置信息浮动层,显示定位状态
This commit is contained in:
@@ -23,6 +23,7 @@ import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.CameraAlt
|
||||
@@ -33,6 +34,8 @@ import androidx.compose.material.icons.filled.FlashOn
|
||||
import androidx.compose.material.icons.filled.LocationOn
|
||||
import androidx.compose.material.icons.filled.Settings
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CardDefaults
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.FloatingActionButton
|
||||
import androidx.compose.material3.Icon
|
||||
@@ -90,6 +93,7 @@ fun CameraScreen(
|
||||
var isCapturing by remember { mutableStateOf(false) }
|
||||
var flashMode by remember { mutableIntStateOf(ImageCapture.FLASH_MODE_AUTO) }
|
||||
var locationText by remember { mutableStateOf("") }
|
||||
var isLocationLoading by remember { mutableStateOf(true) }
|
||||
var manualAddress by remember { mutableStateOf("") }
|
||||
var currentWatermarkStyle by remember { mutableStateOf(WatermarkStyle.Default) }
|
||||
var currentImageQuality by remember { mutableStateOf(ImageQuality.Standard) }
|
||||
@@ -128,17 +132,31 @@ fun CameraScreen(
|
||||
}
|
||||
}
|
||||
|
||||
// 请求权限
|
||||
LaunchedEffect(Unit) {
|
||||
if (!permissionsState.allPermissionsGranted) {
|
||||
permissionsState.launchMultiplePermissionRequest()
|
||||
}
|
||||
}
|
||||
|
||||
// 获取位置
|
||||
LaunchedEffect(hasLocationPermission) {
|
||||
if (hasLocationPermission) {
|
||||
LaunchedEffect(permissionsState.allPermissionsGranted, hasLocationPermission) {
|
||||
isLocationLoading = true
|
||||
if (permissionsState.allPermissionsGranted && hasLocationPermission) {
|
||||
try {
|
||||
Log.d("CameraScreen", "Getting location...")
|
||||
locationText = locationHelper.getLocationInfo()
|
||||
Log.d("CameraScreen", "Location result: $locationText")
|
||||
} catch (e: Exception) {
|
||||
locationText = ""
|
||||
Log.e("CameraScreen", "Location error", e)
|
||||
locationText = "定位失败"
|
||||
}
|
||||
} else if (manualAddress.isNotBlank()) {
|
||||
locationText = manualAddress
|
||||
} else {
|
||||
locationText = ""
|
||||
}
|
||||
isLocationLoading = false
|
||||
}
|
||||
|
||||
Box(modifier = Modifier.fillMaxSize()) {
|
||||
@@ -169,7 +187,9 @@ fun CameraScreen(
|
||||
onGalleryClick = onNavigateToGallery,
|
||||
onMergeClick = { if (capturedImages.isNotEmpty()) onNavigateToMerge(capturedImages.toList()) },
|
||||
capturedCount = capturedImages.size,
|
||||
isCapturing = isCapturing
|
||||
isCapturing = isCapturing,
|
||||
locationText = locationText,
|
||||
isLocationLoading = isLocationLoading
|
||||
)
|
||||
} else {
|
||||
PermissionRequest(
|
||||
@@ -190,7 +210,9 @@ private fun CameraContent(
|
||||
onGalleryClick: () -> Unit,
|
||||
onMergeClick: () -> Unit,
|
||||
capturedCount: Int,
|
||||
isCapturing: Boolean
|
||||
isCapturing: Boolean,
|
||||
locationText: String,
|
||||
isLocationLoading: Boolean
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
val lifecycleOwner = LocalLifecycleOwner.current
|
||||
@@ -263,6 +285,44 @@ private fun CameraContent(
|
||||
isCapturing = isCapturing,
|
||||
modifier = Modifier.align(Alignment.BottomCenter)
|
||||
)
|
||||
|
||||
// 位置信息浮动层
|
||||
if (isLocationLoading || locationText.isNotBlank()) {
|
||||
Card(
|
||||
modifier = Modifier
|
||||
.align(Alignment.BottomCenter)
|
||||
.padding(bottom = 120.dp, start = 16.dp, end = 16.dp),
|
||||
colors = CardDefaults.cardColors(
|
||||
containerColor = Color.Black.copy(alpha = 0.6f)
|
||||
)
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
if (isLocationLoading) {
|
||||
CircularProgressIndicator(
|
||||
modifier = Modifier.size(16.dp),
|
||||
color = Color.White,
|
||||
strokeWidth = 2.dp
|
||||
)
|
||||
} else {
|
||||
Icon(
|
||||
imageVector = Icons.Default.LocationOn,
|
||||
contentDescription = null,
|
||||
tint = Color.White,
|
||||
modifier = Modifier.size(16.dp)
|
||||
)
|
||||
}
|
||||
Spacer(modifier = Modifier.width(4.dp))
|
||||
Text(
|
||||
text = if (isLocationLoading) "正在定位..." else locationText,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = Color.White
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import android.content.Context
|
||||
import android.location.Geocoder
|
||||
import android.location.Location
|
||||
import android.os.Looper
|
||||
import android.util.Log
|
||||
import com.google.android.gms.location.FusedLocationProviderClient
|
||||
import com.google.android.gms.location.LocationCallback
|
||||
import com.google.android.gms.location.LocationRequest
|
||||
@@ -67,13 +68,16 @@ class LocationHelper(private val context: Context) {
|
||||
* 获取位置信息(地址或经纬度)
|
||||
*/
|
||||
suspend fun getLocationInfo(useNetwork: Boolean = true): String {
|
||||
Log.d("LocationHelper", "Getting location, useNetwork=$useNetwork")
|
||||
if (!useNetwork) {
|
||||
val location = getCurrentLocation() ?: return ""
|
||||
return "${"%.4f".format(location.latitude)}, ${"%.4f".format(location.longitude)}"
|
||||
val location = getCurrentLocation()
|
||||
Log.d("LocationHelper", "GPS location: $location")
|
||||
return location?.let { "${"%.4f".format(it.latitude)}, ${"%.4f".format(it.longitude)}" } ?: ""
|
||||
}
|
||||
|
||||
val location = getCurrentLocation() ?: return ""
|
||||
return getAddressFromLocation(location.latitude, location.longitude)
|
||||
val location = getCurrentLocation()
|
||||
Log.d("LocationHelper", "Network location: $location")
|
||||
return location?.let { getAddressFromLocation(it.latitude, it.longitude) } ?: ""
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user