Search code examples
androidgoogle-mapsmarker

How to Display marker title for a few seconds after its creation and then remove it?


I have a MapActivity in my Android application. I have added marker to a particular location as

mMap.addMarker(new MarkerOptions()
                        .title(values[1])
                        .position(stepsInThePath.get(Integer.valueOf(values[0])).startLocation));

But the title appear only when the marker is clicked.

What I want?

I want to show the title on the marker as soon as the marker is created (i.e, without clicking). The title should disappear after... say 5 seconds.

Any kind of help will be greatly appreciated.

Thanks!


Solution

  • Try this one,

        final Marker marker = mMap.addMarker(new MarkerOptions()
                            .title(values[1])
                            .position(stepsInThePath.get(Integer.valueOf(values[0])).startLocation));
                marker.showInfoWindow();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (marker!=null)
                        {
                           //If you only hide marker title
                           marker.hideInfoWindow();
    
                          //If you want to remove marker
                            marker.remove();
    
                        }
                    }
                }, 5000);//Millisecond for remove marker