Custom marker in google maps in android with vector asset icon

前端 未结 9 1207
小鲜肉
小鲜肉 2020-12-02 09:50

How can we achieve a map marker icon with vector asset file, the way google shows it like this, programatically:

Update:

m         


        
9条回答
  •  执念已碎
    2020-12-02 10:12

    In Kotlin: I used the below code to show the SVG image on Marker. Here, I used no background color / SVG.

    fun getBitmapDescriptorFromVector(context: Context, @DrawableRes vectorDrawableResourceId: Int): BitmapDescriptor? {
    
        val vectorDrawable = ContextCompat.getDrawable(context, vectorDrawableResourceId)
        val bitmap = Bitmap.createBitmap(vectorDrawable!!.intrinsicWidth, vectorDrawable.intrinsicHeight, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(bitmap)
        vectorDrawable.setBounds(0, 0, canvas.width, canvas.height)
        vectorDrawable.draw(canvas)
    
        return BitmapDescriptorFactory.fromBitmap(bitmap)
    }
    

    Use like this way:

    googleMap?.addMarker(MarkerOptions().position(LatLng(it.latitude!!, it.longitude!!))
                .title(it.airLineDetails))?.setIcon(
                getBitmapDescriptorFromVector(requireContext(), R.drawable.ic_flight_blue))
    

    Screen Shot:

提交回复
热议问题