2

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.

  • 2
    Please add more source code: 1) with `lastClicked` declaration and changes, 2) `getMarkerIcon()` implementation. – Andrii Omelchenko Jan 26 '21 at 09:45
  • @AndriiOmelchenko I edited and added code for getMarkerIcon(). And lastClicked declaration. – Parteek Singh Bedi Jan 26 '21 at 18:52
  • 1
    Seems everything in code that you added is ok. Try double check marker creation part may be several markers with same coordinates added. – Andrii Omelchenko Jan 26 '21 at 19:08
  • 1
    @AndriiOmelchenko Thank you so much for your suggestion, after your comment I checked my list and compared few markers and I found out that there were few markers with exactly same lat and lng, so markers were getting overlapped. – Parteek Singh Bedi Jan 26 '21 at 22:11

3 Answers3

1

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.

0

you should try this out

googlemap.addMarker(new MarkerOptions()
.position(new LatLng( 65.07213,-2.109375))
.title("This is my title")
.snippet("and snippet")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));

Hope this will work for you!

D_K
  • 152
  • 2
  • 9
0

Probably at marker creation step several markers with same coordinates added and overlap each other. To check marker existence by coordinates you can use O(1) complexity (instead of O(N) for iterating over all markers case) approach by using LatLng of Marker object as HashMap/HashSet key. For do that you need to implement approach described, for example, in this answer of Tomasz Nurkiewicz. For example, Key class can be like this:

public class LatLngKey {
    private LatLng mLatLng;

    public LatLngKey(LatLng latLng) {
        mLatLng = latLng;
    }

    public double getLattitude() {
        return mLatLng.latitude;
    }

    public double getLongitude() {
        return mLatLng.longitude;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof LatLngKey)) return false;
        LatLngKey key = (LatLngKey) o;
        return mLatLng.latitude == key.getLattitude() && mLatLng.longitude == key.getLongitude();
    }

    @Override
    public int hashCode() {
        // for better accuracy it is needed to convert double values into integer 
        int result = (int) mLatLng.latitude * 10_000;
        result = 31 * result + (int) mLatLng.longitude * 10_000;
        return result;
    }
}

and using of it like that:

...
mGoogleMap = googleMap;

Map<LatLngKey, Marker> markersMap = new HashMap<>();
LatLng positionNoida = new LatLng(28.57, 77.32);
Marker markerNoida = mGoogleMap.addMarker(new MarkerOptions().position(positionNoida)
        .title("Marker in Noida"));
LatLngKey markerKey = new LatLngKey(markerNoida.getPosition());
markersMap.put(markerKey, markerNoida);

LatLng positionNoida2 = new LatLng(28.57, 77.32);
Marker markerNoida2 = mGoogleMap.addMarker(new MarkerOptions().position(positionNoida2)
        .title("Marker in Noida 2"));
LatLngKey markerKey2 = new LatLngKey(markerNoida2.getPosition());

if (markersMap.containsKey(markerKey2)) {
    // marker with same coordinates exists 
    ...
} else {
    // marker with same coordinates do not exists 
    ...
}
...

You need to implement PharmacyKey class for your Pharmacy.

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79