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).
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.
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))));
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 Drawable
s. 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.
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);
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;
}