Home » Android » android – Change marker size in Google Maps API v2

android – Change marker size in Google Maps API v2

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’m trying to port my app to the brand new Google Maps API v2, but can’t find how to change the size of the marker (some of my markers are smaller than default).

In v1, I used a Drawable which I scaled with setBounds() before adding it to the map.

But now, in v2, I can’t use a Drawable. I’ve to use MarkerOptions().icon(), which takes just a BitmapDescriptor (generated with a BitmapDescriptorFactory).

Looking at the reference, there doesn’t seem to be any support for setting or changing the BitmapDescriptor size.

So, have I missed something, or is it just plain impossible to set the size for custom markers in this API version?

How to&Answers:

You can first convert it into Bitmap and change its size and then use that bitmap in as a custom marker. For example I first created a method which accepts name of your image file in drawable folder, and width and height of marker you want to set.

public Bitmap resizeMapIcons(String iconName,int width, int height){
    Bitmap imageBitmap = BitmapFactory.decodeResource(getResources(),getResources().getIdentifier(iconName, "drawable", getPackageName()));
    Bitmap resizedBitmap = Bitmap.createScaledBitmap(imageBitmap, width, height, false);
    return resizedBitmap;
}

Then call like this in setUpMap() method to create a new marker of required size.

googleMap.addMarker(new MarkerOptions()
            .title("New Marker")
            .snippet("Check out this place.")
            .position(chelsea).icon(BitmapDescriptorFactory.fromBitmap(resizeMapIcons("image_name",100,100))));

Answer:

Best solution I’ve found is to resize the Bitmap just before adding it as a Marker. For example, in my code I use a LevelListDrawable which references several multiple-resolution Drawables. Since I want half-size Markers, I do:

LevelListDrawable d=(LevelListDrawable) getResources().getDrawable(R.drawable.estado_variable);
d.setLevel(1234);
BitmapDrawable bd=(BitmapDrawable) d.getCurrent();
Bitmap b=bd.getBitmap();
Bitmap bhalfsize=Bitmap.createScaledBitmap(b, b.getWidth()/2,b.getHeight()/2, false);
mapa.addMarker(new MarkerOptions()
        .position(POSITION)
        .title("Title")
        .icon(BitmapDescriptorFactory.fromBitmap(bhalfsize))
        );

This way, I can keep using Drawables while been able to obtain differently sized markers just converting them to Bitmap and resizing as needed.

Answer:

It seems the only way to do it is by setting a custom Marker image.

From API Reference: If you’d like to change more than just the color of the marker, you can set a custom marker image, often called an icon. Custom icons are always set as a BitmapDescriptor, and defined using one of four methods in the BitmapDescriptorFactory class.

Answer:

public Bitmap bitmapSizeByScall( Bitmap bitmapIn, float scall_zero_to_one_f) {

    Bitmap bitmapOut = Bitmap.createScaledBitmap(bitmapIn,
            Math.round(bitmapIn.getWidth() * scall_zero_to_one_f),
            Math.round(bitmapIn.getHeight() * scall_zero_to_one_f), false);

    return bitmapOut;
}

Bitmap size returns to 80% of the original.

Bitmap resizeBitmap = bitmapSizeByScall(originBitmap, 0.8f);

Answer:

Just a quick snippet that works for me:

private Bitmap scaleImage(Resources res, int id, int lessSideSize) {
    Bitmap b = null;
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;

    BitmapFactory.decodeResource(res, id, o);

    float sc = 0.0f;
    int scale = 1;
    // if image height is greater than width
    if (o.outHeight > o.outWidth) {
        sc = o.outHeight / lessSideSize;
        scale = Math.round(sc);
    }
    // if image width is greater than height
    else {
        sc = o.outWidth / lessSideSize;
        scale = Math.round(sc);
    }

    // Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    b = BitmapFactory.decodeResource(res, id, o2);
    return b;
}

Answer:

Resize drawable image of marker

int height = 80;
int width = 60;
BitmapDrawable bitmapdraw=(BitmapDrawable)getResources().getDrawable(R.mipmap.marker);
Bitmap b=bitmapdraw.getBitmap();
Bitmap smallMarker = Bitmap.createScaledBitmap(b, width, height, false);

use the bitmap in marker icon

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