Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 930
Mithun Sarker Shuvro Avatar asked Nov 28 '16 09:11

Mithun Sarker Shuvro


1 Answers

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;
}
like image 59
Dima Rostopira Avatar answered Sep 28 '22 15:09

Dima Rostopira