Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom marker in google maps in android with vector asset icon

How can we achieve a map marker icon with vector asset file, the way google shows it like this, programatically:

map

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.

like image 294
Shuddh Avatar asked Feb 21 '17 11:02

Shuddh


People also ask

How do I start a custom image on Google Maps marker for mobile app?

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.


Video Answer


2 Answers

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)         }     } 
like image 192
Leonid Ustenko Avatar answered Oct 31 '22 03:10

Leonid Ustenko


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.

Screenshot for end result

like image 29
SidMorad Avatar answered Oct 31 '22 05:10

SidMorad