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

5

46

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. How can i specify the size of the marker in dp so that i can control how it looks like on the map

Vicious answered 1/3, 2016 at 8:38 Comment(0)
T
127

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))
);
Triad answered 1/3, 2016 at 8:52 Comment(0)
S
29

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)
Seeker answered 14/6, 2019 at 14:30 Comment(0)
B
9

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)
}
Baese answered 22/5, 2020 at 14:58 Comment(0)
M
0

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.

Mimosaceous answered 1/3, 2016 at 9:1 Comment(0)
H
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);
}
Headpin answered 30/1, 2018 at 13:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.