Hide markers info window in android google maps API v2

19,435

Solution 1

return true;

from onMarkerClick to disable default behavior of showing info window and centering on the marker.

Solution 2

If you use clusters, write so:

private var clusterManager: ClusterManager<SomeClusterItem>? = null

override fun onMapReady(googleMap: GoogleMap) {
    this.googleMap = googleMap

    clusterManager = ClusterManager(context!!, googleMap)
    ... // Other clusterManager and clusterRenderer initializations.

    clusterManager!!.setOnClusterItemClickListener { item ->
        // selectMarker(item)
        true // false, if you want to show InfoWindow.
    }
}
Share:
19,435

Related videos on Youtube

MaciejGórski
Author by

MaciejGórski

"This is programming, anything is possible" Developer of Vielen Games - turn-based multiplayer game server (quoridor) Hrisey - boilerplate removal tool for Android, Android Maps Extensions - marker clustering with smooth API, Yafi - Internet Chess - app with ugly or no design and a lot of apps for many clients.

Updated on June 03, 2022

Comments

  • MaciejGórski
    MaciejGórski almost 2 years

    Currently I was able to view all my markers in Google maps using Android Google maps API v2.

    Adding my marker in map:

    mapView.addMarker
         (new MarkerOptions()
          .position(aUsersLocation).
          icon(BitmapDescriptorFactory.fromBitmap(aUserImage))
          .snippet(My_VALUE_1)
          .title(My_VALUE_2)
         .hideInfoWindow();
    

    I have several markers and assigned few values (My_VALUE_1 and My_VALUE_2) to each marker's snippet and title. When user clicks a marker, I need these unique value and I will receive these values in onMarkerClick listener as:

            @Override
            public boolean onMarkerClick(Marker theMarker) 
            {
                String aValue1 = theMarker.getSnippet();
                String aValue2 = theMarker.getTitle();
                theMarker.getPosition().latitude...
               ...
                return false;
            }
    

    My question is: as I am adding the snippet and title values to the marker, when user clicks the marker, infoWindow is displayed.

    I need to hide the marker's infoWindow. I tried with hideInfoWindow, but it seems to be not working.

    Any suggestions please.

    Thank You.

  • saschoar
    saschoar almost 11 years
    By the way, if you'd still like to keep the map center animation to the marker (but just want to keep the info window hidden), just add mapView.animateCamera(CameraUpdateFactory.newLatLng(marker.g‌​etPosition())); before you return true;
  • deimos1988
    deimos1988 about 10 years
    Thank you both very much for your answer/comment!
  • Hitesh Dhamshaniya
    Hitesh Dhamshaniya over 7 years
    What if I just wanted to show info window but not move center animation. can you please suggest solution for that problem.
  • MaciejGórski
    MaciejGórski over 7 years
    @HiteshDhamshaniya Then I guess you call marker.showInfoWindow() and return true which indicates the click is handled by your code.
  • Hitesh Dhamshaniya
    Hitesh Dhamshaniya over 7 years
    @MaciejGórski Thanks reply.