import
android.content.Context
import
android.os.Bundle
import
androidx.activity.ComponentActivity
import
androidx.activity.compose.setContent
import
androidx.compose.foundation.background
import
androidx.compose.foundation.layout.*
import
androidx.compose.material.*
import
androidx.compose.runtime.Composable
import
androidx.compose.runtime.mutableStateOf
import
androidx.compose.runtime.remember
import
androidx.compose.ui.Modifier
import
androidx.compose.ui.graphics.Color
import
androidx.compose.ui.platform.LocalContext
import
androidx.compose.ui.text.style.TextAlign
import
androidx.compose.ui.unit.dp
import
androidx.compose.ui.viewinterop.AndroidView
import
com.example.newcanaryproject.ui.theme.NewCanaryProjectTheme
import
com.example.newcanaryproject.ui.theme.greenColor
import
com.google.android.libraries.maps.CameraUpdateFactory
import
com.google.android.libraries.maps.GoogleMap
import
com.google.android.libraries.maps.MapView
import
com.google.android.libraries.maps.model.LatLng
import
com.google.android.libraries.maps.model.MarkerOptions
import
com.google.maps.android.ktx.awaitMap
import
kotlinx.coroutines.CoroutineScope
import
kotlinx.coroutines.Dispatchers
import
kotlinx.coroutines.launch
class
MainActivity : ComponentActivity() {
var message =
""
override fun onCreate(savedInstanceState: Bundle?) {
super
.onCreate(savedInstanceState)
setContent {
NewCanaryProjectTheme {
Surface(
modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background
) {
Scaffold(
topBar = {
TopAppBar(backgroundColor = greenColor,
title = {
Text(
text =
"GFG"
,
modifier = Modifier.fillMaxWidth(),
textAlign = TextAlign.Center,
color = Color.White
)
})
}) {
mapUI(LocalContext.current)
}
}
}
}
}
}
@Composable
fun mapUI(context: Context) {
val mapView = rememberMapViewWithLifecycle()
Column(
modifier = Modifier.fillMaxHeight().fillMaxWidth().background(Color.White)
) {
Row(
modifier = Modifier.fillMaxWidth().padding(all =
5
.dp),
horizontalArrangement = Arrangement.Center
) {
Button(
onClick = {
getMapLocation(mapView,
"Hybrid"
)
},
modifier = Modifier
.padding(
5
.dp)
.width(
120
.dp)
) {
Text(text =
"Hybrid"
, textAlign = TextAlign.Center)
}
Button(
onClick = {
getMapLocation(mapView,
"Terrain"
)
},
modifier = Modifier.padding(
5
.dp).width(
120
.dp)
) {
Text(text =
"Terrain"
, textAlign = TextAlign.Center)
}
Button(
onClick = {
getMapLocation(mapView,
"Satellite"
)
},
modifier = Modifier.padding(
5
.dp).width(
120
.dp)
) {
Text(text =
"Satellite"
, textAlign = TextAlign.Center)
}
}
AndroidView({ mapView }) { mapView ->
CoroutineScope(Dispatchers.Main).launch {
val map = mapView.awaitMap()
map.uiSettings.isZoomControlsEnabled =
true
}
getMapLocation(mapView,
""
)
}
}
}
private
fun getMapLocation(mapView: MapView, mapType: String) {
mapView.getMapAsync {
if
(mapType.equals(
"Hybrid"
)) {
it.mapType = GoogleMap.MAP_TYPE_HYBRID
}
else
if
(mapType.equals(
"Terrain"
)) {
it.mapType = GoogleMap.MAP_TYPE_TERRAIN
}
else
if
(mapType.equals(
"Satellite"
)) {
it.mapType = GoogleMap.MAP_TYPE_SATELLITE
}
val delhi = LatLng(
28.644800
,
77.216721
)
it.addMarker(MarkerOptions().position(delhi).title(
"Marker at Delhi"
))
it.animateCamera(CameraUpdateFactory.zoomTo(
20
.0f))
it.moveCamera(CameraUpdateFactory.newLatLng(delhi))
}
}