Search code examples
androidgoogle-mapsgoogle-maps-markersinfowindow

Turn off marker movement to the center of map Android


I am using a customize InfoWindow in Google Map using InfoWindowAdapter. Every time I click on a marker, it moves to the center of the mobile screen and open the info window. I was wondering if it is possible to stop the marker movement from the current location to the center. I am using the following code:

googleMap.setInfoWindowAdapter(new UserInfoWindowAdapter(getLayoutInflater()));
googleMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
    @Override
    public void onInfoWindowClick(Marker marker) {      
        //other Code
    }
});

Solution

  • Ok first the code:

    mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        Marker mLastMarker;
        @Override
        public boolean onMarkerClick(Marker marker) {
            if (marker.equals(mLastMarker)) {
                // Same marker clicked twice - hide InfoWindow
                mLastMarker = null;
                marker.hideInfoWindow();
            } else {
                mLastMarker = marker;
                marker.showInfoWindow();
            }
            return true;
        }
    });
    

    Secondly the functionality that the above code provides:

    • Overrides the default OnMarkerClick functionality (return true;)
    • Imitates isInfoWindowShown functionality as there is a known bug to that method
    • With the help of the previous point, toggles the InfoWindow visibility

    Good luck!