How to specify the size of the icon on the Marker in Google Maps V2 Android
The accepted answer is outdated (Resources::getDrawable
has been deprecated since API level 22). 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
.icon(smallMarkerIcon)
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.
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:
map.addMarker(new MarkerOptions()
.position(POSITION)
.title("Your title")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker))
);
Kotlin version I used 0- 9 answer and used it with 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)
}