Custom marker in google maps in android with vector asset icon

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

    convert vector resource to bitmap object and use BitmapDescriptorFactory.fromBitmap(bitmap)

       Bitmap bitmap = getBitmapFromVectorDrawable(getContext(),R.drawable.ic_pin);
       BitmapDescriptor descriptor =BitmapDescriptorFactory.fromBitmap(bitmap);
       MarkerOptions markerOptions = new MarkerOptions();
       markerOptions.icon(descriptor);
    

    Bitmap converter:

     public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
            Drawable drawable =  AppCompatResources.getDrawable(context, drawableId)
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                drawable = (DrawableCompat.wrap(drawable)).mutate();
            }
    
            Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
                    drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            drawable.draw(canvas);
    
            return bitmap;
        }
    

提交回复
热议问题