Essential Android Google Maps edit forum

InfoWindow Click Listener

Here is an example of how to define a different action for each Marker’s InfoWindow click event.

Use a HashMap in which the marker ID is the key, and the value is the corresponding action it should take when the InfoWindow is clicked.

Then, use a OnInfoWindowClickListener to handle the event of a user clicking the InfoWindow, and use the HashMap to determine which action to take.

In this simple example we will open up a different Activity based on which Marker’s InfoWindow was clicked.

Declare the HashMap as an instance variable of the Activity or Fragment:

//Declare HashMap to store mapping of marker to Activity
HashMap<String, String> markerMap = new HashMap<String, String>();

Then, each time you add a Marker, make an entry in the HashMap with the Marker ID and the action it should take when it’s InfoWindow is clicked.

For example, adding two Markers and defining an action to take for each:

Marker markerOne = googleMap.addMarker(new MarkerOptions().position(latLng1)
        .title("Marker One")
        .snippet("This is Marker One");
String idOne = markerOne.getId();
markerMap.put(idOne, "action_one");

Marker markerTwo = googleMap.addMarker(new MarkerOptions().position(latLng2)
        .title("Marker Two")
        .snippet("This is Marker Two");
String idTwo = markerTwo.getId();
markerMap.put(idTwo, "action_two");

In the InfoWindow click listener, get the action from the HashMap, and open up the corresponding Activity based on the action of the Marker:

mGoogleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
 @Override
 public void onInfoWindowClick(Marker marker) {

   String actionId = markerMap.get(marker.getId());

   if (actionId.equals("action_one")) {
     Intent i = new Intent(MainActivity.this, ActivityOne.class);
     startActivity(i);
   } else if (actionId.equals("action_two")) {
     Intent i = new Intent(MainActivity.this, ActivityTwo.class);
     startActivity(i);
   }
 }
});

Note If the code is in a Fragment, replace MainActivity.this with getActivity().


Table Of Contents
23 Google Maps
39 ACRA
64 Menu
112 Loader
119 Xposed
132 Colors
135 Fresco
140 AdMob
147 Button
156 Vk SDK
170 XMPP
176 OpenCV
200 FileIO
203 Moshi
217 Paint
231 AIDL
241 JCodec
243 Okio
255 Looper
  ↑ ↓ to navigate     ↵ to select     Esc to close