Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Google Map V2: How to change previous clicked marker's icon when clicked on another marker

UPDATE: I have solved the performance issue by adding a previousMarker object. So only the previous clicked marker will be remove and replaced with the default icon. However the info window is still not shown when I click on the marker.


I have a map view and set some markers on it. What I want is when I clicked on a marker, it changes its icon to be a different icon, and when I click on another marker, the previous marker's icon should change to its original one.

What I've done is something like this but it just simply changes the marker icon whenever I click on the marker.

@Override
public boolean onMarkerClick(Marker marker) { //Called when a marker has been clicked or tapped.

    LatLng markerPos=marker.getPosition();
    String markerLocationName=marker.getTitle();
    String markerSubCategoryName=marker.getSnippet();

    marker.remove();

    MarkerOptions markerOptions =
            new MarkerOptions().position(markerPos)
                    .title(markerLocationName)
                    .snippet(markerSubCategoryName)
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.new_icon));// Changing marker icon
    mMap.addMarker(markerOptions);
    Log.d("marker","change marker icon"); // can open a dialog window here
    return false;
}

So if I click 2 markers, I will get 2 new icons appears, meanwhile what I want is only the current clicked marker changes its icon.

So I've also done something like this by adding 2 more lines of code. It succeeds doing what I want but it has some drawback (see below).

@Override
public boolean onMarkerClick(Marker marker) { //Called when a marker has been clicked or tapped.

    mMap.clear();
    populateAllMarkersOnMap();//repopulate markers on map

    LatLng markerPos=marker.getPosition();
    String markerLocationName=marker.getTitle();
    String markerSubCategoryName=marker.getSnippet();

    marker.remove(); //remove the current clicked marker

    MarkerOptions markerOptions =
            new MarkerOptions().position(markerPos)
                    .title(markerLocationName)
                    .snippet(markerSubCategoryName)
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.new_icon));// Changing marker icon
    mMap.addMarker(markerOptions); //add marker with new icon into map
    return false;
}

The drawback is 1/ it "disable" the info window (the same thing also happen in the first way). 2/ it clear all the markers on map and set all the markers again. Imagine I have 100 markers, should that be a performance problem on every click I do?

The populateAllMarkersOnMap() can be something as simple like this at the moment:

private void populateAllMarkersOnMap(){
    setMarker(latA1, lonA1, "A1","A1.1"); 
    setMarker(latA2, lonA2, "A2","A2.1"); 
    // ... (100 times or populated via a loop) 
};

So is there a way to get previous clicked marker to change its icon back to default when I click a new marker? Apologise for my English, if you think I should put another title for my question, please help.

like image 318
Great Question Avatar asked Oct 04 '14 14:10

Great Question


Video Answer


1 Answers

Finally I found the best and most simple way. I made a previousMarker object and store the current clicked marker:

@Override
public boolean onMarkerClick(Marker marker) { //Called when a marker has been clicked or tapped.
    if(previousMarker!=null){
        previousMarker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.dot_icon));
    }
    marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.ct_icon));
    previousMarker=marker; //Now the clicked marker becomes previousMarker
    return false;
}
like image 50
Great Question Avatar answered Oct 14 '22 04:10

Great Question