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

Moh*_*dar 29 size android google-maps marker

在may app我使用Google Maps V2中的Map,在这张地图中,我试图用标记为每个Marker添加标记,但是标记占据了图标的大小,使图标看起来像烟.如何在dp中指定标记的大小,以便我可以控制它在地图上的样子

Use*_*ing 93

目前我认为我们无法更改标记大小,因此您可以在drawable中添加标记图像并重新调整大小如下:

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);
Run Code Online (Sandbox Code Playgroud)

你用图标添加标记就像这样

                map.addMarker(new MarkerOptions()
                        .position(POSITION)
                        .title("Your title")
                        .icon(BitmapDescriptorFactory.fromBitmap(smallMarker))
                );
Run Code Online (Sandbox Code Playgroud)


O95*_*O95 5

Approved answer is outdated (getDrawable(), depricated since API level 22), so I changed it a litte bit:

int height = 100;
int width = 100;
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.FOO);
Bitmap smallMarker = Bitmap.createScaledBitmap(b, width, height, false);
BitmapDescriptor smallMarkerIcon = BitmapDescriptorFactory.fromBitmap(smallMarker);
Run Code Online (Sandbox Code Playgroud)

and then apply it in MarkerOption

.icon(smallMarkerIcon)
Run Code Online (Sandbox Code Playgroud)


Dag*_*ois 5

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)
}
Run Code Online (Sandbox Code Playgroud)