marker.setIcon throws java.lang.IllegalArgumentException: Unmanaged descriptor
Asked Answered
V

2

6

I am having trouble with changing googlemap marker on the googlemap.

Neither this method works

MarkerOptions markerOptions = new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(MarkerManager.getBitmapFromVectorDrawable(context, R.drawable.marker_no_issues)));
                    marker.setIcon(markerOptions.getIcon());

nor this

marker.setIcon(BitmapDescriptorFactory.fromBitmap(MarkerManager.getBitmapFromVectorDrawable(context, R.drawable.marker_no_issues)));

Method getBitmapFromVectorDrawable:

public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
    Drawable drawable = AppCompatDrawableManager.get().getDrawable(context, drawableId);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        drawable = (DrawableCompat.wrap(drawable)).mutate();
    }

    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
            drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}

What i am doing wrong? Why i cant change marker icon with setIcon method. I have also tried raw .png files and i know that vector to bitmap works well.

Vasilikivasilis answered 21/5, 2017 at 20:52 Comment(1)
This is a duplicate of #41902978.Tentmaker
B
4

I had the same problem solved this as follows - before setting the icon to the marker object just check if the marker is still visible on googlemap If you had cleared the map and setting the icon to this marker object on map, would result in this error

In that case, just create another new object instead of updating the same object by setter methods

Becky answered 7/6, 2017 at 11:1 Comment(1)
How we can check google map if marker is still visible? I have this crash and problem from popping up of fragment from back stack and when what to update again Markers this crash happens.Narcho
T
0

As said in https://mcmap.net/q/277347/-illegalargumentexception-unmanaged-descriptor-using-gms-maps-model-marker-seticon: "Try not to setIcon() on old marker, instead inflate new marker and then use setIcon()".

In my case I tried to replace bitmaps of selected and unselected markers when clicking on them (see https://mcmap.net/q/658634/-how-to-change-the-clicked-marker-icon-using-android-maps-utils). Probably the exception raised when a previous marker was invisible, as @Kaveri said above, I don't know.

Currently I get a previous marker not with a saved reference, but with setting again.

private var selectedItem: StationClusterItem? = null

override fun onMapReady(googleMap: GoogleMap) {
    ...

    val clusterRenderer = MarkerClusterRenderer(context!!, googleMap, clusterManager!!,
        unselectedBitmap!!)
    clusterManager?.setOnClusterItemClickListener { item ->
        if (selectedItem != null) {
            // Set here a reference to a previous marker.
            // We save a reference to a previous item, not to a marker.
            val lastMarker = clusterRenderer.getMarker(selectedItem)
            lastMarker?.setIcon(unselectedBitmap)
        }
        selectedItem = item
        // Now get a reference to a selected marker.
        val newMarker = clusterRenderer.getMarker(item)
        newMarker?.setIcon(selectedBitmap)
        false
    }
}
Tentmaker answered 18/12, 2018 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.