7

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.

philomath
  • 2,209
  • 6
  • 33
  • 45

3 Answers3

18

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;
}
philomath
  • 2,209
  • 6
  • 33
  • 45
  • I write some code that finally work like this, but it has a problem when I go out and come back to the page. I have no idea whats wrong whit my code – Ashkan Jul 11 '17 at 08:09
  • If you use clusters, it will change a cluster circle with marker image. – CoolMind Dec 18 '18 at 09:01
0

You might be looking for this method probably

Called when the marker's info window is closed.
optional public func mapView(mapView: GMSMapView, didCloseInfoWindowOfMarker marker: GMSMarker)
ZaEeM ZaFaR
  • 1,508
  • 17
  • 22
0

I found the best and most simple way. I made another marker object and store the current clicked marker enter code here

@Override
public boolean onMarkerClick(Marker marker) { //Called when a marker has been clicked or tapped.
    if(previousMarker!=null){
        marker2.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.dot_icon));
    }
    marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.ct_icon));
    marker2=marker; //Now the clicked marker becomes previousMarker
    return false;
}