I've been trying to add a shape drawable as the marker icon for a marker I want to add on the map.
shape looks like this (res/drawable/blue_circle.xml):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<size
android:width="15dp"
android:height="15dp" />
<solid
android:color="@color/Blue" />
</shape>
and I try to add the marker like this:
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.blue_circle));
Apparently I get a NullPointer exception.
Must the marker icon be a bitmap?
Am I allowed to add shapes drawables as marker icons?
And if yes what am I doing wrong?
Create a drawable for your marker (res/drawable/map_dot_red.xml):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<gradient
android:angle="90"
android:endColor="#f58383"
android:startColor="#ee6464" />
<stroke
android:width="1dp"
android:color="#a13939" />
</shape>
Create a bitmap from the drawable:
int px = getResources().getDimensionPixelSize(R.dimen.map_dot_marker_size);
mDotMarkerBitmap = Bitmap.createBitmap(px, px, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(mDotMarkerBitmap);
Drawable shape = getResources().getDrawable(R.drawable.map_dot_red);
shape.setBounds(0, 0, mDotMarkerBitmap.getWidth(), mDotMarkerBitmap.getHeight());
shape.draw(canvas);
Create your marker, using the bitmap:
Marker marker = mMap.addMarker(new MarkerOptions()
.position(point)
.anchor(.5f, .5f)
.icon(BitmapDescriptorFactory.fromBitmap(mDotMarkerBitmap)));
Set the size of your marker in dimens (res/values/dimens.xml):
<resources>
<dimen name="map_dot_marker_size">12dp</dimen>
</resources>
Here's an answer if you're using the com.google.maps.android:android-maps-utils
library.
Create a Bitmap using IconGenerator
IconGenerator iconGen = new IconGenerator(context);
// Define the size you want from dimensions file
int shapeSize = getResources().getDimensionPixelSize(R.dimen.shape_size);
Drawable shapeDrawable = ResourcesCompat.getDrawable(getResources(), R.drawable.my_shape, null);
iconGen.setBackground(shapeDrawable);
// Create a view container to set the size
View view = new View(context);
view.setLayoutParams(new ViewGroup.LayoutParams(shapeSize, shapeSize));
iconGen.setContentView(view);
// Create the bitmap
Bitmap bitmap = iconGen.makeIcon();
Then use the bitmap like you would when setting the marker icon BitmapDescriptorFactory.fromBitmap(bitmap)
My way to deal with shape (xml) drawables as marker icons (based on saxman answer).
I have a lot of objects (markers) to show on map. They have both kinds of drawables – bitmap and shape. The same icon my used for multiple markers. Thus, I have added some caching for bitmap descriptors and drawable type detection.
SparseArray<BitmapDescriptor> iconCache = new SparseArray<>();
for (MyObject object : objects) {
int iconResId = object.icon;
BitmapDescriptor icon = iconCache.get(iconResId);
if (icon == null) {
Drawable drawable;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
drawable = getResources().getDrawable(iconResId, null);
} else {
drawable = getResources().getDrawable(iconResId);
}
if (drawable instanceof GradientDrawable) {
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
drawable.draw(canvas);
icon = BitmapDescriptorFactory.fromBitmap(bitmap);
bitmap.recycle();
} else {
icon = BitmapDescriptorFactory.fromResource(iconResId);
}
iconCache.put(iconResId, icon);
map.addMarker(new MarkerOptions()
.icon(icon)
.position(position));
}
}
My shape drawable have set size, thus, I can query drawable for desired size for bitmap.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size android:width="12dp" android:height="12dp" />
<solid android:color="@color/transit_bus" />
</shape>