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

45

在我的应用中,我使用了 Google Maps V2 中的地图,并且我试图在这个地图上添加一个带有图标的标记,但是标记会采取图标的大小,这使得图标看起来很占空间。 我该如何指定标记的大小为 dp ,以便我可以控制它在地图上的外观呢?

5个回答

126

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

创建调整后的 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))
);

29

被接受的答案已经过时(自API 22级别以来,Resources :: getDrawable已被弃用),以下是更新版本:

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)

9

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

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

0

我认为你可以在这个问题上寻找答案,它已经解释了如何通过创建动态位图来创建自定义标记,给定宽度和高度。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接