package
com.example.newcanaryproject
import
android.content.Context
import
android.location.Address
import
android.location.Geocoder
import
android.os.Bundle
import
android.widget.Toast
import
androidx.activity.ComponentActivity
import
androidx.activity.compose.setContent
import
androidx.compose.foundation.Image
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.Alignment
import
androidx.compose.ui.Modifier
import
androidx.compose.ui.graphics.Color
import
androidx.compose.ui.platform.LocalContext
import
androidx.compose.ui.res.painterResource
import
androidx.compose.ui.text.TextStyle
import
androidx.compose.ui.text.style.TextAlign
import
androidx.compose.ui.unit.dp
import
androidx.compose.ui.unit.sp
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.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
import
java.io.IOException
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()
val locationName = remember {
mutableStateOf(
""
)
}
Column(
modifier = Modifier
.fillMaxHeight()
.fillMaxWidth()
.background(Color.White)
) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(all =
3
.dp),
horizontalArrangement = Arrangement.Center
) {
TextField(
value = locationName.value,
onValueChange = { locationName.value = it },
placeholder = { Text(text =
"Enter your location to search"
) },
modifier = Modifier
.padding(
3
.dp)
.width(
300
.dp)
.height(
60
.dp),
textStyle = TextStyle(color = Color.Black, fontSize =
15
.sp),
singleLine =
true
,
)
Spacer(modifier = Modifier.width(
5
.dp))
Button(
onClick = {
getMapLocation(locationName.value, context, mapView)
},
modifier = Modifier.padding(
3
.dp)
) {
Image(
painter = painterResource(id = R.drawable.ic_search),
alignment = Alignment.Center,
contentDescription =
"Image"
,
modifier = Modifier
.height(
40
.dp)
.width(
40
.dp)
)
}
}
AndroidView({ mapView }) { mapView ->
CoroutineScope(Dispatchers.Main).launch {
val map = mapView.awaitMap()
map.uiSettings.isZoomControlsEnabled =
true
}
}
}
}
private
fun getMapLocation(location: String, context: Context, mapView: MapView) {
var addressList: List<Address>? =
null
mapView.getMapAsync {
if
(location !=
null
|| location ==
""
) {
val geocoder = Geocoder(context)
try
{
addressList = geocoder.getFromLocationName(location,
1
)
}
catch
(e: IOException) {
e.printStackTrace()
}
val address: Address = addressList!![
0
]
val latLng = LatLng(address.getLatitude(), address.getLongitude())
it.addMarker(
MarkerOptions().position(
latLng
).title(
"Marker in "
+ location)
)
it.moveCamera(
CameraUpdateFactory.newLatLng(
latLng
)
)
it.moveCamera(
CameraUpdateFactory.newLatLngZoom(
latLng, 8f
)
)
}
}
}