shuvro shuvro - 11 days ago 7
Android Question

Android change google map markers icon on click

I want to change the marker icon in google map on click . I can do it easily by overriding

onMarkerClick


@Override
public boolean onMarkerClick(Marker marker) {

marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.ic_selected_user_mark_icon));

return true;
}


But the problem is, lets say I have already clicked a marker which icon is already changed . When I click another marker it's icon also changed . What I actually want only clicked marker has a different icon . And the others will have same.

Answer

Save last clicked marker, and on second click restore it's icon to default

Marker lastClicked = null;
@Override
public boolean onMarkerClick(Marker marker) {
    if (lastClicked!=null)
        lastClicked.setIcon(<defaultIcon>);
    marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.ic_selected_user_mark_icon));
    lastClicked = marker;
    return true;
}