Change marker size in Google Maps API v2
Asked Answered
C

5

35

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?

Comparator answered 13/2, 2013 at 10:41 Comment(1)
Solved. If I use a smaller Bitmap, or if I convert a Drawable to Bitmap and scale it before adding it as a Marker, I get a smaller marker with all the benefits of Drawables. I will post an example when system allows me to do it (I'm quite new and I've to wait a few hours before answering myself :-) ).Comparator
I
59

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))));
Incumber answered 24/8, 2015 at 17:10 Comment(0)
C
17

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.

Comparator answered 14/2, 2013 at 11:7 Comment(1)
java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable cannot be cast to android.graphics.drawable.LevelListDrawableBeore
L
6

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.

Liquorice answered 13/2, 2013 at 10:50 Comment(3)
You're right, if I use a smaller bitmap (lower resolution PNG), the marker is smaller too. Just a shame we can't use Drawables directly, but it works. Thanks!Comparator
You're welcome :) I faced the same issue when I wanted to draw text on my markers.Liquorice
I have a tiny map and the marker has same height as my MapView :(Expectorant
G
6
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);
Graeco answered 15/7, 2016 at 12:48 Comment(1)
Example: mBitmap width = 100, then Bitmap asdas width = 80.Graeco
O
1

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;
}
Orelle answered 28/4, 2014 at 13:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.