Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
144 views
in Technique[技术] by (71.8m points)

android - Marker sometimes change colour on click and sometimes it does not

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I solved this problem by checking my list of markers. From that I found that there were markers with exactly same lat and lng, that's why markers were overlapped, that means there were more than 1 markers on one

 public boolean isDuplicate(Pharmacy pharmacy){

        for(int i=0; i<pharmacyList.size(); i++){

            if(pharmacyList.get(i).getLatitude() == pharmacy.getLatitude()

                    &&pharmacyList.get(i).getLongitude() == pharmacy.getLongitude())

                return true;

        }

        return false;

    }

NOTE: Pharmacy is class that lat and lng properties.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...