简体   繁体   中英

Custom Marker Icon Image Url GoogleMap Compose Android

GoogleMap composes support maker icons but does not help get from Url, I hope some coding helps customer properties Icons image easy more. But If you have a solution good more, please comment here

Code in GoogleMap compose

val bitmapState = remember {
                mutableStateOf<BitmapDescriptor?>(null)
            }
            LaunchedEffect(key1 = locationFocus.value.imageUrl) {
                getBitmapFromURL(locationFocus.value.imageUrl)?.let { bm ->
                    getResizedBitmap(bm, 150, 150)?.let {
                        getRoundedCornerBitmap(it)?.let {
                            bitmapState.value =
                                BitmapDescriptorFactory.fromBitmap(it)
                        }
                    }
                }
            }

            Marker(
                state = MarkerState(position = cameraPositionState.position.target),
                title = locationFocus.value.name,
                snippet = locationFocus.value.address,
                icon = bitmapState.value,
            )
        }

//Get Bitmap from Url
suspend fun getBitmapFromURL(imgUrl: String?): Bitmap? =
    withContext(Dispatchers.IO) {
        try {
            val url = URL(imgUrl)
            val connection: HttpURLConnection =
                url.openConnection() as HttpURLConnection
            connection.doInput = true
            connection.connect()
            val input: InputStream = connection.inputStream
            BitmapFactory.decodeStream(input)
        } catch (e: IOException) {
            // Log exception
            null
        }
    }
//Resize Image Bitmap
fun getResizedBitmap(bm: Bitmap, newWidth: Int, newHeight: Int): Bitmap? {
    ...
    bm.recycle()
    return resizedBitmap
}

//Border Bitmap
fun getRoundedCornerBitmap(bitmap: Bitmap): Bitmap? {
   ...
    return output
}

If you have another code simple please comment. Good job

Image result marker icon google map compose. 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM