Home » Android » android – Why marker's info window doesn't show?

android – Why marker's info window doesn't show?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am stuck couldn’t figour out why info window doesn’t show up when I click on map’s marker. I read on developer android site that only should I add marker and give them title, snippet and so on. But the result is nothing.

public class ClubMapActivity extends DefaultActivity implements GoogleMap.OnMarkerClickListener {
    private GoogleMap mMap;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.clubmap);
        Bundle bundle = getIntent().getExtras();
        double lat = bundle.getDouble("latitude");
        double lng = bundle.getDouble("longitude");


        mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        mMap.addMarker(new MarkerOptions()
                .position(new LatLng(lat, lng))
                .title(bundle.getString("clubNmae")).snippet("AAA"));

        animateCameraTo(lat, lng);
        CameraUpdate zoom=CameraUpdateFactory.zoomTo(20);

        mMap.animateCamera(zoom);


    }

    public void animateCameraTo(final double lat, final double lng)
    {
        CameraUpdate center=
                CameraUpdateFactory.newLatLng(new LatLng(lat, lng));
        CameraUpdate zoom=CameraUpdateFactory.zoomTo(18);

        mMap.moveCamera(center);
        mMap.animateCamera(zoom);
    }


    @Override
    public boolean onMarkerClick(Marker marker) {
        if(marker.isInfoWindowShown()) {
            marker.hideInfoWindow();
        } else {
            marker.showInfoWindow();
        }
        return true;
    }
}
How to&Answers:

By default, an info window is displayed when a user taps on a marker if the marker has a title set.

Make sure your .title(bundle.getString("clubNmae") is returning not null value otherwise you cannot see the info window when clicking on the marker.

Answer:

It doesn’t look like you are calling setOnMarkerClickListener.

https://developers.google.com/maps/documentation/android/marker

Answer:

Adding to @fish40 answer above: title must be not only NotNull but also NotEmpty, i.e. even if you pass “” as the marker title, the info window will not show up.

Answer:

Try This code, it worked for me….

XML File

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:id="@+id/showTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@android:color/black"
                android:textSize="20sp"
                android:textStyle="bold"
                android:hint="Current Location"
              />

            <TextView
                android:id="@+id/showLat"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="Latitude"/>

            <TextView
                android:id="@+id/showLong"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="Longitued"/>
        </LinearLayout>


    </LinearLayout>

JAVA CODE

     if(mMap !=null) {
                mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
                    @Override
                    public View getInfoWindow(Marker marker) {
                        return null;
                    }

                    @Override
                    public View getInfoContents(Marker marker) {

                        View v = getLayoutInflater().inflate(R.layout.info_window,null);
                        TextView showTitle = (TextView)  v.findViewById(R.id.showSnappest);
                        TextView showLat   = (TextView) v.findViewById(R.id.showLat);
                        TextView showLong  = (TextView) v.findViewById(R.id.showLong);

                        LatLng latLng = marker.getPosition();
                        showTitle.setText(marker.getTitle());
                        showLat.setText("Latitude:"+latLng.latitude);
                        showLong.setText("Longitude:"+latLng.longitude);
                        return v;
                    }
                });
            }

Answer:

I’ve had another case of this, mind that if you call GoogleMap.clear() all adapters set in setInfoWindowAdapter will gone. You need to set them again after calling clear

Answer:

Use as this:

GoogleMap mMap;
MarkerOptions options = new MarkerOptions();
Marker marker = mMap.addMarker(options.position(new LatLng(lat,lng)));
marker.showInfoWindow();

showInfoWindow() method is available at Marker object, not
MarkerOptions object.

Answer:

In my case info window was not showing because I was setting OnMarkerClickListener and OnInfoWindowClickListener at the same time and MarkerClickListener was returning true, If you change it to false or remove MarkerClickListener, info window is seen again.