Google Maps Marker for Google: Unknown Link Released

I am trying to add tablet support in my application and throw in the IllegalArgumentException thrown by this line of code:

marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.arrow_green_10by19)) 

The .fromResource method works great with the R.drawable.arrow_green_10by19 file from the image file (png), but when the png is replaced with the arrow_green_10by19.xml vector file (which renders perfectly in the Android Studio IDE), it generates a runtime environment as mentioned.

Does anyone know how to implement a vector resource in BitmapDescriptorFactory and can help me?

Thanks.

+5
source share
1 answer

I had the same problem, but I realized that on my device with API 16 it works fine, but with API 21 it crashes. Finally, it works on both devices using this solution . Here is the code:

 @TargetApi(Build.VERSION_CODES.LOLLIPOP) private static Bitmap getBitmap(VectorDrawable vectorDrawable) { Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); vectorDrawable.draw(canvas); return bitmap; } 

and this:

 private static Bitmap getBitmap(Context context, int drawableId) { Drawable drawable = ContextCompat.getDrawable(context, drawableId); if (drawable instanceof BitmapDrawable) { return BitmapFactory.decodeResource(context.getResources(), drawableId); } else if (drawable instanceof VectorDrawable) { return getBitmap((VectorDrawable) drawable); } else { throw new IllegalArgumentException("unsupported drawable type"); } } 

So, I combined these 2 functions in this way:

 private Marker addMark(LatLng latLng, String title) { Bitmap bitmap = getBitmap(getContext(), R.drawable.ic_place_24dp); Marker marker = googleMap.addMarker(new MarkerOptions().position(latLng) .title(title) .icon(BitmapDescriptorFactory.fromBitmap(bitmap)) .draggable(true)); return marker; } 

Where R.drawable.ic_place_24dp is a vector asset (.xml), not .png

+14
source

Source: https://habr.com/ru/post/1234454/


All Articles