GoogleMap支持制作图标,但不帮助从Url获取,我希望一些编码可以帮助客户属性图标图像更容易。但如果你有更好的解决方案,请在这里评论
GoogleMap 中的代码撰写
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
}
如果您有其他简单的代码,请评论。干得好