带有矢量资产图标的android中的Google地图中的自定义标记 [英] Custom marker in google maps in android with vector asset icon

查看:126
本文介绍了带有矢量资产图标的android中的Google地图中的自定义标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何使用矢量资产文件来实现地图标记图标,Google会以这种方式通过编程方式显示它:

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

更新:

map.addMarker(new MarkerOptions()
    .position(latLng)
    .icon(BitmapDescriptorFactory.fromResource(R.drawable.your_vector_asset))
    .title(title);

这在处理矢量资产时不起作用.提出问题的主要原因.上面的代码有错误:

this doesnot work when dealing with vector assets. The main reason to ask the question. The error with the above code:

java.lang.IllegalArgumentException:无法解码图像.提供的图像必须是位图.

java.lang.IllegalArgumentException: Failed to decode image. The provided image must be a Bitmap.

推荐答案

我一直在寻找完全相同的要求,看到这个问题起初让我很高兴,但与@Shuddh一样,我对给出的答案不满意.

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);
}

和用法示例:

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

注意:您可能希望对向量使用不同的边界,我的向量大小为24dp,并且我使用了48dp png图像(蓝色部分,也可以是向量)作为背景.

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.

这篇关于带有矢量资产图标的android中的Google地图中的自定义标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆