UPDATE: I solved the performance issue by adding the previousMarker object. Thus, only the previous click marker will be deleted and replaced with the default icon. However, when I click on the marker, the info window still does not appear.
I have a map view and some markers are installed on it. What I want is when I click on the marker, it changes its icon to another icon, and when I click on another marker, the previous marker icon should be replaced with its original one.
I did something like this, but just 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 press 2 markers, I will have 2 new icons, meanwhile what I want, only the current clicked marker changes its icon.
So, I also did something like this, adding 2 more lines of code. It successfully accomplishes what I want, but has some flaw (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 disadvantage is 1 / "disable" the information window (the same thing happens in the first case). 2 / clear all the markers on the map and reinstall all the markers. Imagine I have 100 markers, should this be a performance issue with every click I make?
PopulateAllMarkersOnMap () can be as simple as it is now:
private void populateAllMarkersOnMap(){ setMarker(latA1, lonA1, "A1","A1.1"); setMarker(latA2, lonA2, "A2","A2.1");
So, is there a way to get the previous marker to change its default icon when I click on a new marker? Sorry for my English, if you think I should put another heading for my question, please help.