I have this application where I show markers on google maps for pharmacies nearby. When I click on marker I want the marker to change the colour. When I click on some other marker, it should change the previous marker's colour to default and will change new marker's colour.
This is working randomly, I mean sometimes marker colour is getting changed and sometimes it stays the default color.
Marker lastClicked;
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in user's location
// User's location is taken from the postal code entered
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
if(lastClicked != null){
lastClicked.setIcon(BitmapDescriptorFactory.defaultMarker());
}
marker.setIcon(getMarkerIcon(getResources().getColor(R.color.app_color)));
lastClicked=marker;
return false;
}
});
}
Code for getMarkerIcon() method is:
public BitmapDescriptor getMarkerIcon(int color){
float[] hsvFloat = new float[3];
Color.colorToHSV(color, hsvFloat);
return BitmapDescriptorFactory.defaultMarker(hsvFloat[0]);
}
NOTE: I added debugger in every line to see what part of code does not run, and it is strange that when debugger come to this line
marker.setIcon(getMarkerIcon(getResources().getColor(R.color.app_color)));
it is getting compiled and yet sometimes it does not change the color of the marker.
question from:
https://stackoverflow.com/questions/65895430/marker-sometimes-change-colour-on-click-and-sometimes-it-does-not 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…