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().