1

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

Ho Binh
  • 351
  • 1
  • 3
  • 8

1 Answers1

0

Image result marker icon google map compose. enter image description here

Ho Binh
  • 351
  • 1
  • 3
  • 8
  • Can you share the source codes just for the marker? – Vahit Keskin Apr 02 '23 at 11:52
  • Do you want the source code to make RoundedCorner? Because Marker is a widget available by GoogleMap this code only loads Icon bitmap custom. – Ho Binh May 15 '23 at 10:25
  • I would be very happy if I could reach all of them. – Vahit Keskin May 16 '23 at 20:58
  • fun getResizedBitmap(bm: Bitmap, newWidth: Int, newHeight: Int): Bitmap? { val width = bm.width val height = bm.height val scaleWidth = newWidth.toFloat() / width val scaleHeight = newHeight.toFloat() / height // CREATE A MATRIX FOR THE MANIPULATION val matrix = Matrix() // RESIZE THE BIT MAP matrix.postScale(scaleWidth, scaleHeight) // "RECREATE" THE NEW BITMAP val resizedBitmap = Bitmap.createBitmap( bm, 0, 0, width, height, matrix, false) bm.recycle() return resizedBitmap } – Ho Binh May 26 '23 at 08:21
  • fun getRoundedCornerBitmap(bitmap: Bitmap): Bitmap? { val w=bitmap.width val h=bitmap.height val radius = (h / 2).coerceAtMost(w / 2) val output = Bitmap.createBitmap(w + 16, h + 16, Bitmap.Config.ARGB_8888) val paint = Paint() paint.isAntiAlias = true val canvas = Canvas(output) canvas.drawARGB(0, 0, 0, 0) paint.style = Paint.Style.FILL canvas.drawCircle((w / 2 + 8).toFloat(), (h / 2 + 8).toFloat(), radius.toFloat(), paint) //continue – Ho Binh May 26 '23 at 08:23
  • paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN) canvas.drawBitmap(bitmap, 4f, 4f, paint) paint.xfermode = null paint.style = Paint.Style.STROKE paint.color = Color.argb(55f,139f,195f,74f) paint.strokeWidth = 10f canvas.drawCircle((w / 2 + 8).toFloat(), (h / 2 + 8).toFloat(), radius.toFloat(), paint) return output } – Ho Binh May 26 '23 at 08:24
  • @VahitKeskin this code custom in here – Ho Binh May 26 '23 at 08:24