简体   繁体   English

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

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

In may app i use Map from Google Maps V2 and in this map i am trying to add markers each Marker with an icon, but the marker is taking the size of the icon which is making the icon looks flue.在 May 应用程序中,我使用 Google Maps V2 中的 Map,在这张地图中,我试图为每个 Marker 添加带有图标的标记,但标记采用了图标的大小,这使图标看起来很流畅。 How can i specify the size of the marker in dp so that i can control how it looks like on the map我如何在dp 中指定标记的大小,以便我可以控制它在地图上的外观

Currently it's not possible to specify a marker size using MarkerOptions , so your only option is to rescale your Bitmap before setting it as your marker icon.目前无法使用MarkerOptions指定标记大小,因此您唯一的选择是在将Bitmap设置为标记图标之前重新缩放Bitmap

Creating the scaled 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);

Using smallMarker as the marker icon:使用smallMarker作为标记图标:

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

The accepted answer is outdated ( Resources::getDrawable has been deprecated since API level 22).已接受的答案已过时(自 API 级别 22 起不推荐使用Resources::getDrawable )。 Here's an updated version:这是一个更新的版本:

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

and then apply it in MarkerOption然后将其应用到 MarkerOption

.icon(smallMarkerIcon)

Kotlin version I used 0- 9 answer and used it with kotlin 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)
}

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM