Open In App

How to Add OnClickListener to Marker on Google Maps in Android?

Last Updated : 23 Jan, 2023
Like Article

We have seen implementing Marker on Google Maps in Android. Now we will see adding OnClickListener for that marker on our Google Maps. In this article, we will take a look at adding OnClickListener to Google Maps marker in Android. 

What we are going to build in this article? 

We will be building a simple application in which we will be showing a marker on a specific location on a map and we will be adding OnClickListener to the marker on Google Maps. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language. 

Add OnClickListener to Marker on Google Maps in Android Sample GIF

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language. Make sure to select Maps Activity while creating a new Project.

Step 2: Generating an API key for using Google Maps

To generate the API key for Maps you may refer to How to Generate API Key for Using Google Maps in Android. After generating your API key for Google Maps. We have to add this key to our Project. For adding this key in our app navigate to the values folder > google_maps_api.xml file and at line 23 you have to add your API key in the place of YOUR_API_KEY

Step 3: Adding OnClickListener to Marker in Google Maps

Go to the file and refer to the following code. Below is the code for the file. Comments are added inside the code to understand the code in more detail.


import android.os.Bundle;
import android.widget.Toast;
import java.util.ArrayList;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
    private GoogleMap mMap;
    // below are the latitude and longitude
    // of 4 different locations.
    LatLng sydney = new LatLng(-34, 151);
    LatLng TamWorth = new LatLng(-31.083332, 150.916672);
    LatLng NewCastle = new LatLng(-32.916668, 151.750000);
    LatLng Brisbane = new LatLng(-27.470125, 153.021072);
    // two array list for our lat long and location Name;
    private ArrayList<LatLng> latLngArrayList;
    private ArrayList<String> locationNameArraylist;
    protected void onCreate(Bundle savedInstanceState) {
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(;
        // initializing our array lists.
        latLngArrayList = new ArrayList<>();
        locationNameArraylist = new ArrayList<>();
        // on below line we are adding
        // data to our array list.
        locationNameArraylist.add("New Castle");
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        // below line is to add marker to google maps
        for (int i = 0; i < latLngArrayList.size(); i++) {
            // adding marker to each location on google maps
            mMap.addMarker(new MarkerOptions().position(latLngArrayList.get(i)).title("Marker in " + locationNameArraylist.get(i)));
            // below line is use to move camera.
        // adding on click listener to marker of google maps.
        mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
            public boolean onMarkerClick(Marker marker) {
                // on marker click we are getting the title of our marker
                // which is clicked and displaying it in a toast message.
                String markerName = marker.getTitle();
                Toast.makeText(MapsActivity.this, "Clicked location is " + markerName, Toast.LENGTH_SHORT).show();
                return false;

After adding this code. Now run your app and see the output of the app.


Note: In the Google Developer Console (, ensure that the “Google Maps Android API v2” is enabled. And also ensure that your API Key exists.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads