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

半腔热情 提交于 2019-11-28 20:22:50

Currently i think we cannot change the marker size, so u can add marker image in drawable and re-size something like this:,

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

Ur add marker be like this with icon

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

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

and then apply it in MarkerOption

.icon(smallMarkerIcon)
Andrea Montanari

I think you can look for an answer on this question, where it already explained how to create a custom marker, with a given width and height by creating a dynamic bitmap.

Rohit Bansal

[edit: formatting]

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);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!