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):
Create a bitmap from the drawable:
Create your marker, using the bitmap:
Set the size of your marker in dimens (res/values/dimens.xml):
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.
My shape drawable have set size, thus, I can query drawable for desired size for bitmap.
Here's an answer if you're using the
com.google.maps.android:android-maps-utils
library.Create a Bitmap using
IconGenerator
Then use the bitmap like you would when setting the marker icon
BitmapDescriptorFactory.fromBitmap(bitmap)