I added a mMap.setOnMarkerClickListener(this);
in the onMapReady(GoogleMap googleMap)
method. So every time you click a marker it displays the text name in the toast method.
public class DemoMapActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener,OnMapReadyCallback, GoogleMap.OnMarkerClickListener {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_places);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
double lat=0.34924212701428;
double lng=32.616554024713;
String venue = "Capital Shoppers City";
LatLng location = new LatLng(lat, lng);
mMap.addMarker(new MarkerOptions().position(location).title(venue)).setTag(0);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLng(location);
CameraUpdate zoom = CameraUpdateFactory.zoomTo(16);
mMap.moveCamera(cameraUpdate);
mMap.animateCamera(zoom);
mMap.setOnMarkerClickListener(this);
}
@Override
public boolean onMarkerClick(final Marker marker) {
// Retrieve the data from the marker.
Integer clickCount = (Integer) marker.getTag();
// Check if a click count was set, then display the click count.
if (clickCount != null) {
clickCount = clickCount + 1;
marker.setTag(clickCount);
Toast.makeText(this,
marker.getTitle() +
" has been clicked ",
Toast.LENGTH_SHORT).show();
}
// Return false to indicate that we have not consumed the event and that we wish
// for the default behavior to occur (which is for the camera to move such that the
// marker is centered and for the marker's info window to open, if it has one).
return false;
}
}
You can check this link for reference Markers