Ok, I got a simple example working. The overall idea here is to use a wrapper class to store the data for each Marker
, and then keep the data stored in a HashMap
with the Marker ID as the key so that you can obtain it in the InfoWindowAdapter
.
First, create a holder class for the info corresponding to each Marker
(you can expand on this to include more info as needed):
public class MarkerHolder {
public String startdate;
public String enddate;
public String fromplace;
public String toplace;
public MarkerHolder(String sd, String ed, String fp, String tp) {
startdate = sd;
enddate = ed;
fromplace = fp;
toplace = tp;
}
}
Then create a HashMap<String, MarkerHolder>
that will map each Marker
ID to the info for each Marker
, and make it an instance variable:
HashMap<String, MarkerHolder> markerHolderMap = new HashMap<String, MarkerHolder>();
Here is a simplified example of just adding one Marker
, note where the info is added to the HashMap
with the Marker
ID as the key:
public void addpaqs() {
//Simple example with just one Marker:
String creatorfirstname = "creator_first_name";
String creatorlastname = "creator_last_name";
String paqtype = "paq_type";
String startdate = "start_date";
String enddate = "end_date";
String fromplace = "from_country";
String toplace = "to_country";
String fromcity = "from_city";
String tocity = "to_city";
//String price = row.getString("price");
double tolongdouble = -122.417506;
double tolatdouble = 37.77657;
MarkerOptions options = new MarkerOptions();
options.position(new LatLng(tolatdouble, tolongdouble));
//options.Price(price);
//options.icon(BitmapDescriptorFactory.fromResource(R.drawable.paqqyinactive));
options.title(paqtype);
options.snippet(creatorfirstname + " " + creatorlastname);
Marker marker = mGoogleMap.addMarker(options);
MarkerHolder mHolder = new MarkerHolder(startdate, enddate, fromplace, toplace);
markerHolderMap.put(marker.getId(), mHolder); //Add info to HashMap
}
Here is the custom layout xml for the InfoWindow
, you can expand on this as needed:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
android:orientation="vertical"
android:background="#d3d3d3">
<TextView
android:id="@+id/paq"
android:textColor="#D3649F"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/names"
android:textColor="#D3649F"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/dates"
android:textColor="#D3649F"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/places"
android:textColor="#D3649F"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Then, put it all together, here is the InfoWindowAdapter
. Note that I used the Title and the Snippet stored in the Marker
, but also used info obtained from the MarkerHolder
which was obtained from the HashMap
:
mGoogleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker arg0) {
return null;
}
@Override
public View getInfoContents(Marker arg0) {
View v = getLayoutInflater().inflate(R.layout.customlayout2, null);
TextView tLocation = (TextView) v.findViewById(R.id.paq);
TextView tSnippet = (TextView) v.findViewById(R.id.names);
TextView tDates = (TextView) v.findViewById(R.id.dates);
TextView tPlaces = (TextView) v.findViewById(R.id.places);
//These are standard, just uses the Title and Snippet
tLocation.setText(arg0.getTitle());
tSnippet.setText(arg0.getSnippet());
//Now get the extra info you need from the HashMap
//Store it in a MarkerHolder Object
MarkerHolder mHolder = markerHolderMap.get(arg0.getId()); //use the ID to get the info
tDates.setText(mHolder.startdate + " " + mHolder.enddate);
tPlaces.setText(mHolder.fromplace + " " + mHolder.toplace);
return v;
}
});
Result: