Sanjay
10/18/2024, 2:39 PMZach Klippenstein (he/him) [MOD]
10/18/2024, 4:22 PMSanjay
10/19/2024, 4:39 AM@OptIn(ExperimentalMaterial3Api::class, ExperimentalPermissionsApi::class)
@Composable
fun MapScreen(navController: NavHostController) {
val (addressQuery, setAddressQuery) = remember { mutableStateOf("") }
var initialLocation = remember { mutableStateOf(LatLng(0.132828, 100.804833)) }
val cameraPositionState = rememberCameraPositionState {
position = CameraPosition.fromLatLngZoom(initialLocation.value, 5f)
}
val locationPermission = rememberMultiplePermissionsState(
listOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION)
)
var uiSettings = MapUiSettings(
compassEnabled = true,
rotationGesturesEnabled = true,
scrollGesturesEnabled = true,
tiltGesturesEnabled = false,
zoomControlsEnabled = false,
zoomGesturesEnabled = true,
mapToolbarEnabled = false,
myLocationButtonEnabled = false,
scrollGesturesEnabledDuringRotateOrZoom = true,
indoorLevelPickerEnabled = true
)
var mapProperties = MapProperties(
maxZoomPreference = 15f,
minZoomPreference = 1f,
isTrafficEnabled = false,
mapType = MapType.NORMAL,
/* latLngBoundsForCameraTarget = LatLngBounds(
LatLng(53.2017, -3.0257),
LatLng(53.2840, -2.5325)
),*/
isMyLocationEnabled = locationPermission.allPermissionsGranted,
)
var mapColorScheme = remember { mutableStateOf(ComposeMapColorScheme.FOLLOW_SYSTEM) }
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior(rememberTopAppBarState())
var marker = remember { MarkerState(position = initialLocation.value) }
Scaffold(
modifier = Modifier.fillMaxSize(),
floatingActionButton = {
FloatingActionButton(
onClick = {
if (locationPermission.allPermissionsGranted) {
cameraPositionState.position = CameraPosition.fromLatLngZoom(initialLocation.value, 15f)
} else {
locationPermission.launchMultiplePermissionRequest()
}
},
contentColor = if (locationPermission.allPermissionsGranted) LogoBlue else Gray
) {
Icon(
painter = painterResource(R.drawable.ic_gps),
contentDescription = "Calibrate to current location",
tint = LogoBlue
)
}
},
topBar = {
CenterAlignedTopAppBar(
title = {
Image(
painter = painterResource(id = R.drawable.ic_pocket_small),
contentDescription = null
)
}, colors = TopAppBarDefaults.centerAlignedTopAppBarColors(
containerColor = MaterialTheme.colorScheme.primaryContainer,
titleContentColor = MaterialTheme.colorScheme.onPrimary,
),
navigationIcon = {
IconButton(onClick = {
navController.navigateUp()
}) {
Icon(
imageVector = Icons.AutoMirrored.Filled.ArrowBack,
contentDescription = "back arrow",
tint = MaterialTheme.colorScheme.onPrimary
)
}
},
scrollBehavior = scrollBehavior
)
}
) { paddingValues ->
if (locationPermission.allPermissionsGranted) {
LocationFetcher { location, string ->
location?.let {
initialLocation.value = LatLng(it.latitude, it.longitude)
cameraPositionState.position = CameraPosition.fromLatLngZoom(LatLng(it.latitude, it.longitude), 15f)
}
}
}
Box(Modifier.padding(top = paddingValues.calculateTopPadding())) {
GoogleMap(
modifier = Modifier.fillMaxSize(),
cameraPositionState = cameraPositionState,
mapColorScheme = mapColorScheme.value,
uiSettings = uiSettings,
properties = mapProperties,
) {
}
Column(
Modifier
.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally
) {
CustomSearchView(
addressQuery,
onValueChange = {
println(it)
setAddressQuery(it)
}) {
addressList()
}
}
}
}
}