tomtomMap.getMarkerSettings().updateMarkerIcon(marker, icon)

Hi,

I am experiencing a very strange issue with
tomtomMap.getMarkerSettings().updateMarkerIcon(marker, icon) method.

First let me explain how I used the method in my activity. I tried multiple scenarios.

case 1) icon is obtained from resource

Icon icon = Icon.Factory.fromResources(context, R.drawable.marker1);
tomtomMap.getMarkerSettings().updateMarkerIcon(marker, icon); 

I did not have any problem in this case.

case 2) icon is obtained from drawable

Drawable drawable = getResources().getDrawable(R.drawable.marker1);
Icon icon = Icon.Factory.fromDrawable("marker1", drawable);
tomtomMap.getMarkerSettings().updateMarkerIcon(marker, icon);

I had a strange issue this one. The icon was updated fine when the above lines were called once, but when I called the above lines twice consecutively, the icon was NOT updated during the second run.

case 3) icon is obtained from LayerDrawable

Drawable drawable1 = getResources().getDrawable(R.drawable.marker_layer1);
Drawable drawable2 = getResources().getDrawable(R.drawable.marker_layer2);
Drawable[] layers = new Drawable[2];
layers[0] = drawable1;
layers[1] = drawable2;
Icon icon = Icon.Factory.fromLayerDrawable("marker1", layers);
tomtomMap.getMarkerSettings().updateMarkerIcon(marker, icon);

I got the same result as in case 2. The icon was updated during first run, but the icon was NOT updated during the second run.

In my app, I am updating the marker icon with LayerDrawable multiple times, so I need to fix this issue. I am guessing the issue is related to the passing context?

Could you take a look at this issue, and let me know how I can fix this?

I found one more thing about this issue.

This problem occurs when the marker was created with below code.

Drawable drawable = getResources().getDrawable(R.drawable.marker1)
//or alternatively creating drawable instance like this
//Drawable drawable = ResourcesCompat.
getDrawable(getResources(), R.drawable.marker1, null);

MarkerBuilder markerBuilder = new MarkerBuilder(position)
.icon(Icon.Factory.fromDrawable(“name”, drawable))
.markerBalloon(balloon)
.iconAnchor(MarkerAnchor.Bottom)
.decal(true);

So if you are trying to reproduce this issue, note that the markerBuilder instance is created with .icon(Icon.Factory.fromDrawable(“name”, drawable)) line.

Hi.

Unfortunately, I can’t reproduce your issue. Please take a look at the following piece of code:

    @Override
    public void onMapClick(@NonNull LatLng latLng) {
        this.tomtomMap.getMarkerSettings().updateMarkerIcon(this.marker, Icon.Factory.fromDrawable("asdf",getResources().getDrawable(R.drawable.ic_markedlocation, null)));
    }
    @Override
    public void onMapLongClick(@NonNull LatLng latLng) {
        Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher, null);
        MarkerBuilder markerBuilder = new MarkerBuilder(latLng)
                .icon(Icon.Factory.fromDrawable("ic_launcher_foreground", drawable));
        this.marker = tomtomMap.addMarker(markerBuilder);
    }
    @Override
    public void onMarkerClick(@NonNull Marker marker) {
        this.tomtomMap.getMarkerSettings().updateMarkerIcon(this.marker, Icon.Factory.fromDrawable("asdf1",getResources().getDrawable(R.drawable.ic_favourites, null)));
    }

It works for me each and every time. I’m using the SDK version 2.4458.
Please make sure that you are using different names (in the first parameter) inside Icon.Factory.fromDrawable function.

If it still won’t work, please share your whole activity code and the SDK version.

Regards,
Mateusz

Okay, I finally got it.

As you mentioned, the problem I experienced was caused by having the same name.
(The first parameter in Icon.Factory.fromDrawable(“name”, drawable) method)

I didn’t know the name has to be different for different icons.
So I guess Icon is considered the same if their names are the same, and this is why the icon was not updated.

Thank you for the help, Mateusz.

2 Likes