Custom marker in google maps in android with vector asset icon

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

    I was looking for exact same requirement, and seeing this question made me happy at first, but same as @Shuddh I wasn't happy with the given answers.

    To make my story short, I am using following code for this requirement:

    private BitmapDescriptor bitmapDescriptorFromVector(Context context, @DrawableRes  int vectorDrawableResourceId) {
        Drawable background = ContextCompat.getDrawable(context, R.drawable.ic_map_pin_filled_blue_48dp);
        background.setBounds(0, 0, background.getIntrinsicWidth(), background.getIntrinsicHeight());
        Drawable vectorDrawable = ContextCompat.getDrawable(context, vectorDrawableResourceId);
        vectorDrawable.setBounds(40, 20, vectorDrawable.getIntrinsicWidth() + 40, vectorDrawable.getIntrinsicHeight() + 20);
        Bitmap bitmap = Bitmap.createBitmap(background.getIntrinsicWidth(), background.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        background.draw(canvas);
        vectorDrawable.draw(canvas);
        return BitmapDescriptorFactory.fromBitmap(bitmap);
    }
    

    and a usage example:

    .icon(bitmapDescriptorFromVector(this, R.drawable.ic_car_white_24dp));
    

    Note: you may wish to use different bounding for your vectors, my vectors were 24dp in size and I've used a 48dp png image(blue-part, which can be a vector too) as background.

    UPDATE: Adding screenshot as it was requested.

提交回复
热议问题