I'm currently using jetpack compose to code a pokemon GO like game. I have my markers on my map and I use AsyncImages to get images from the PokeAPI. The problem is I need to give bitmaps to my markers but I don't know how to do. I'd appreciate any help.
I tried to change the Async image as you can see in the screenshot but it's not working like this.
Full code :
class MainActivity : ComponentActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val height = 200
val width = 100
val loader = ImageLoader(this)
val bitmapdraw = BitmapFactory.decodeResource(applicationContext.resources, R.drawable.charac)
val smallMarker = Bitmap.createScaledBitmap(bitmapdraw, width, height, false)
val location = LatLng(47.394144, 0.68484)
Column() {
MapScreen(location)
Row(
modifier = Modifier
.fillMaxWidth()
.height(50.dp)
.background(Color.Green.copy(alpha = 0.1f))
.weight(1f, true),
horizontalArrangement = Arrangement.SpaceAround,
verticalAlignment = Alignment.CenterVertically
) {
Image(painter = painterResource(id = R.drawable.pokedex),
contentDescription = "Pokeball",
modifier = Modifier
.size(50.dp)
.clickable {
val navigate =
Intent(this@MainActivity, PokedexActivity::class.java)
startActivity(navigate)
}
)
Image(painter = painterResource(id = R.drawable.fight),
contentDescription = "Pokeball",
modifier = Modifier
.size(50.dp)
.clickable {
}
)
Image(painter = painterResource(id = R.drawable.pokeball),
contentDescription = "Pokeball",
modifier = Modifier
.size(50.dp)
.clickable {
}
)
}
}
}
}
}
@Composable
fun MapScreen(localisation: LatLng){
val context = LocalContext.current
val locationState = (MarkerState(position= localisation))
val liste = mutableListOf<Int>(1, 2, 3, 4, 3, 2, 3, 5, 3, 3, 2, 3, 2)
GoogleMap(
cameraPositionState = CameraPositionState(CameraPosition(localisation, 18f, 0f, 0f)),
modifier = Modifier
.fillMaxHeight(0.9f)
) {
AddPokemons(localisation = localisation, pokemons = liste)
}
}
@Composable
fun AddPokemons(localisation: LatLng, pokemons: MutableList<Int>)
{
val context = LocalContext.current
val url = "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/132.png"
pokemons.forEach(){
val randLat = Random.nextDouble(-1.0, 1.0) / 500
val randLong = Random.nextDouble(-1.0, 1.0) / 500
val randomLocation = LatLng(localisation.latitude + randLat, localisation.longitude + randLong)
val locationState = (MarkerState(position= randomLocation))
val image = AsyncImage(model = url, contentDescription = "pokemon")
Marker(
state = locationState,
onClick = {
val navigate = Intent(context, Combat::class.java)
startActivity(context, navigate, null)
false
},
icon = BitmapDescriptorFactory.fromBitmap((image as BitmapDrawable).bitmap.copy(Bitmap.Config.ARGB_8888, true))
)
}
}
@Composable
fun Greeting(name: String) {
Text(
text = "Hello $name!"
)
}
@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
Greeting("Mathis")
}