如何在 Google Maps V2 Android 中指定标记上的图标大小

How to specify the size of the icon on the Marker in Google Maps V2 Android

在可能的应用程序中,我使用来自 Google Maps V2 的地图,在这张地图中,我试图为每个带有图标的标记添加标记,但标记采用了制作图标的图标的大小看起来烟道。 我如何在 dp 中指定标记的大小,以便我可以控制它在地图上的样子

目前无法使用 MarkerOptions 指定标记大小,因此您唯一的选择是在将 Bitmap 设置为标记图标之前重新缩放它。

创建缩放位图:

int height = 100;
int width = 100;
BitmapDrawable bitmapdraw = (BitmapDrawable)getResources().getDrawable(R.mipmap.marker);
Bitmap b = bitmapdraw.getBitmap();
Bitmap smallMarker = Bitmap.createScaledBitmap(b, width, height, false);

使用smallMarker作为标记图标:

map.addMarker(new MarkerOptions()
        .position(POSITION)
        .title("Your title")
        .icon(BitmapDescriptorFactory.fromBitmap(smallMarker))
);

我认为您可以在 this question 上寻找答案,其中已经解释了如何通过创建动态 位图[=14 来创建具有给定宽度和高度的自定义标记=].

Drawable circleDrawable = getResources().getDrawable(R.mipmap.primarysplitter);
bitmapDescriptor = getMarkerIconFromDrawable(circleDrawable);

private BitmapDescriptor getMarkerIconFromDrawable(Drawable drawable) {
    Canvas canvas = new Canvas();
    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    canvas.setBitmap(bitmap);
    drawable.setBounds(0, 0, (int)getResources().getDimension(R.dimen._30sdp), (int)getResources().getDimension(R.dimen._30sdp));
    drawable.draw(canvas);
    return BitmapDescriptorFactory.fromBitmap(bitmap);
}

已接受的答案已过时(Resources::getDrawable 自 API 级别 22 起已弃用)。这是一个更新版本:

int height = 100;
int width = 100;
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable. marker);
Bitmap smallMarker = Bitmap.createScaledBitmap(b, width, height, false);
BitmapDescriptor smallMarkerIcon = BitmapDescriptorFactory.fromBitmap(smallMarker);

然后在MarkerOption中应用它

.icon(smallMarkerIcon)

Kotlin 版本 我使用了 0-9 答案并将其与 kotlin

一起使用
fun generateHomeMarker(context: Context): MarkerOptions {
    return MarkerOptions()
        .icon(BitmapDescriptorFactory.fromBitmap(generateSmallIcon(context)))
}

fun generateSmallIcon(context: Context): Bitmap {
    val height = 100
    val width = 100
    val bitmap = BitmapFactory.decodeResource(context.resources, R.drawable.logo)
    return Bitmap.createScaledBitmap(bitmap, width, height, false)
}