How to select and deselect a marker in google maps in android?

前端 未结 1 456
余生分开走
余生分开走 2020-12-21 02:19

I have a list of places which are marked in google maps using Markers. I want to select a Marker so that it will highlight with a different color.

相关标签:
1条回答
  • 2020-12-21 03:01

    I just tested this and it works, just add a Marker reference as an instance variable in order to keep a reference of the last clicked Marker, and each time a new Marker is clicked, set the previous one back to the default color.

    You can also check !marker.equals(prevMarker) before setting the Marker to HUE_BLUE, this will allow a subsequent click on the Marker to set the color back to the default color.

    Instance variable:

    Marker prevMarker;
    

    Click Listener:

    mGoogleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
    
            @Override
            public boolean onMarkerClick(Marker marker) {
    
                aa= marker.getPosition().latitude;
                bb=marker.getPosition().longitude;
                if (prevMarker != null) {
                    //Set prevMarker back to default color
                    prevMarker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
                }
    
                //leave Marker default color if re-click current Marker
                if (!marker.equals(prevMarker)) {
                    marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
                    prevMarker = marker;
                }
                prevMarker = marker;
                return false;
            }
    
        }); 
    
    0 讨论(0)
提交回复
热议问题