34

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?

Pang
  • 9,564
  • 146
  • 81
  • 122
jesjimher
  • 1,175
  • 1
  • 12
  • 17
  • 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 `Drawable`s. 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 :-) ). – jesjimher Feb 13 '13 at 11:50

5 Answers5

57

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))));
anubh
  • 649
  • 7
  • 12
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.

JJD
  • 50,076
  • 60
  • 203
  • 339
jesjimher
  • 1,175
  • 1
  • 12
  • 17
  • 19
    java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable cannot be cast to android.graphics.drawable.LevelListDrawable – Waza_Be Jun 22 '13 at 10:52
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.

Chris Margonis
  • 935
  • 11
  • 16
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);
amiron
  • 721
  • 9
  • 11
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;
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Mike
  • 2,547
  • 3
  • 16
  • 30