Custom marker in google maps in android with vector asset icon

前端 未结 9 1203
小鲜肉
小鲜肉 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:10

    If someone who is looking for in kotlin here is the method for you :

      private fun  bitmapDescriptorFromVector(context: Context, vectorResId:Int):BitmapDescriptor {
                var vectorDrawable = ContextCompat.getDrawable(context, vectorResId);
                vectorDrawable!!.setBounds(0, 0, vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight());
                var bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
                var canvas =  Canvas(bitmap);
                vectorDrawable.draw(canvas);
                return BitmapDescriptorFactory.fromBitmap(bitmap);
    }
    

    above method will convert you vector icon to bitmapdescritor

    map.addMarker(new MarkerOptions()
                    .position(latLng)
                    .icon(bitmapDescriptorFromVector(getActivity(), R.drawable.your_vector_asset))
                    .title(title)
    

    and this one for setting marker for your map thanks for Leo Droidcoder from his answer only i have converted it to kotlin

提交回复
热议问题