Display toolbar for Google Maps marker automatically

samleighton87 picture samleighton87 · Dec 29, 2014 · Viewed 23.3k times · Source

I have a marker and when I open the map it shows the marker. When I click on it it shows the title and a toolbar which includes two buttons on the bottom right of the map which let me launch intents to navigate to the marker or show it in google maps. I would like to have these displayed automatically when the map is opened rather than having the user to click on the marker.

Like this :-) ... enter image description here I can't seem to work out how to do this.

I have tried:

// create marker
MarkerOptions marker = new MarkerOptions().position(
        new LatLng(latitude, longitude)).title("title");

// adding marker
googleMap.addMarker(marker).showInfoWindow();
googleMap.getUiSettings().setMapToolbarEnabled(true);

But this just shows the marker title and a button on the top right to go to my location not the two toolbar intent buttons on the bottom right.

Like this :-( ... enter image description here I'm a bit stuck any ideas?

Answer

Simas picture Simas · Jan 7, 2015

The overlay that appears when a marker is clicked, is created and destroyed on-the-spot implicitly. You can't manually show that (yet).

If you must have this functionality, you can create an overlay over your map with 2 ImageViews, and call appropriate intents when they're clicked:

// Directions
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(
        "http://maps.google.com/maps?saddr=51.5, 0.125&daddr=51.5, 0.15"));
startActivity(intent);
// Default google map
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(
        "http://maps.google.com/maps?q=loc:51.5, 0.125"));
startActivity(intent);

Note: you need to change the coordinates based on Marker's getPosition() and the user's location.

Now to hide the default overlay, all you need to do is return true in the OnMarkerClickListener. Although you'll lose the ability to show InfoWindows and center camera on the marker, you can imitate that simply enough:

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
    @Override
    public boolean onMarkerClick(Marker marker) {
        marker.showInfoWindow();
        mMap.animateCamera(CameraUpdateFactory.newLatLng(marker.getPosition()));
        return true;
    }
});