How can we achieve a map marker icon with vector asset file, the way google shows it like this, programatically:
Update:
map.addMarker(new MarkerOptions() .position(latLng) .icon(BitmapDescriptorFactory.fromResource(R.drawable.your_vector_asset)) .title(title);
this doesnot work when dealing with vector assets. The main reason to ask the question. The error with the above code:
java.lang.IllegalArgumentException: Failed to decode image. The provided image must be a Bitmap.
For adding a custom marker to Google Maps navigate to the app > res > drawable > Right-Click on it > New > Vector Assets and select the icon which we have to show on your Map. You can change the color according to our requirements. After creating this icon now we will move towards adding this marker to our Map.
You can use this method:
private BitmapDescriptor bitmapDescriptorFromVector(Context context, int vectorResId) { Drawable vectorDrawable = ContextCompat.getDrawable(context, vectorResId); vectorDrawable.setBounds(0, 0, vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight()); Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); vectorDrawable.draw(canvas); return BitmapDescriptorFactory.fromBitmap(bitmap); }
So your code will look like:
map.addMarker(new MarkerOptions() .position(latLng) .icon(bitmapDescriptorFromVector(getActivity(), R.drawable.your_vector_asset)) .title(title);
Edit:
In Kotlin it may look like:
private fun bitmapDescriptorFromVector(context: Context, vectorResId: Int): BitmapDescriptor? { return ContextCompat.getDrawable(context, vectorResId)?.run { setBounds(0, 0, intrinsicWidth, intrinsicHeight) val bitmap = Bitmap.createBitmap(intrinsicWidth, intrinsicHeight, Bitmap.Config.ARGB_8888) draw(Canvas(bitmap)) BitmapDescriptorFactory.fromBitmap(bitmap) } }
I was looking for exact same requirement, and seeing this question made me happy at first, but same as @Shuddh I wasn't happy with the given answers.
To make my story short, I am using following code for this requirement:
private BitmapDescriptor bitmapDescriptorFromVector(Context context, @DrawableRes int vectorDrawableResourceId) { Drawable background = ContextCompat.getDrawable(context, R.drawable.ic_map_pin_filled_blue_48dp); background.setBounds(0, 0, background.getIntrinsicWidth(), background.getIntrinsicHeight()); Drawable vectorDrawable = ContextCompat.getDrawable(context, vectorDrawableResourceId); vectorDrawable.setBounds(40, 20, vectorDrawable.getIntrinsicWidth() + 40, vectorDrawable.getIntrinsicHeight() + 20); Bitmap bitmap = Bitmap.createBitmap(background.getIntrinsicWidth(), background.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); background.draw(canvas); vectorDrawable.draw(canvas); return BitmapDescriptorFactory.fromBitmap(bitmap); }
and a usage example:
.icon(bitmapDescriptorFromVector(this, R.drawable.ic_car_white_24dp));
Note: you may wish to use different bounding for your vectors, my vectors were 24dp in size and I've used a 48dp png image(blue-part, which can be a vector too) as background.
UPDATE: Adding screenshot as it was requested.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With