Search code examples
androidgoogle-mapsgoogle-maps-android-api-2infowindow

How to pass more than just title and snippet to getInfoContents in custom InfoWindow for Android


I have a InfoWindowAdapter class in my Android app that refers to an xml layout containing three TextViews. I add a new marker using the code below within an addMarker() method:

mapView.addMarker(new MarkerOptions()
            .position(latLon)
            .title(titleText)
            .snippet(snippetText)
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_green)));

    mapView.setInfoWindowAdapter(new InfoWindow(getLayoutInflater()));
    mapView.setOnInfoWindowClickListener(this);

I then set the text of my two infowindow textviews using marker.getTitle() and marker.getSnippet() within a getInfoContents() method:

@Override
    public View getInfoContents(Marker marker) {

        if (popup == null) {

            popup = inflater.inflate(R.layout.infowindow_popup, null);
        }

        TextView tvTitle = (TextView) popup.findViewById(R.id.title);
        tvTitle.setText(marker.getTitle());

        TextView tvSnippet = (TextView) popup.findViewById(R.id.snippet) ;
        tvSnippet.setText(marker.getSnippet());

        TextView tvSnippet2 = (TextView) popup.findViewById(R.id.snippet_2) ;
        tvSnippet2.setText("test");

        return popup;
    }

This is all good for the first two textviews but what I would like to know is what is the correct way for me to pass a third string to infoWindowContents() to use with tvSnippet2? Obviously I can't use .title()/.snippet() and marker.getTitle()/marker.getSnippet() because these are already used and would repeat the data.

infoWindowAdapter:

public class InfoWindow implements InfoWindowAdapter {

    private View popup = null;
    private LayoutInflater inflater = null;
    String str;

    InfoWindow(LayoutInflater inflater, String s) {

        this.inflater = inflater;

        str = s;
    }

    @Override
    public View getInfoContents(Marker marker) {

        if (popup == null) {

            popup = inflater.inflate(R.layout.infowindow_popup, null);
        }

        TextView tvTitle = (TextView) popup.findViewById(R.id.title);
        tvTitle.setText(marker.getTitle());

        TextView tvSnippet = (TextView) popup.findViewById(R.id.snippet) ;
        tvSnippet.setText(marker.getSnippet());

        TextView tvSnippet2 = (TextView) popup.findViewById(R.id.snippet_2) ;
        tvSnippet2.setText(str);

        return popup;
    }

    @Override
    public View getInfoWindow(Marker marker) {

        return null;
    }



}

Solution

  • When you add Marker in Map at that time concat your all Strings in your Title string like

    String title="First String"+"_"+"Second String";
    

    then add this to your title

    all = mMap.addMarker(new MarkerOptions()
        .icon(BitmapDescriptorFactory
        .fromResource(R.drawable.mark_red))
        .position(Location)
        .title(title)
        .snippet(snippet);
    

    Now, When you click into any Marker then your CustomInfoWindow rising up.So you'll parse your title in getInfoContents(...) like

     @Override
     public View getInfoContents(Marker marker) {
    
        if (popup == null) {
    
            popup = inflater.inflate(R.layout.infowindow_popup, null);
        }
    
        String str=marker.getTitle();
        final String[] str2=str.split("_");
    
        TextView tvTitle = (TextView) popup.findViewById(R.id.title);
        tvTitle.setText(str2[0]);// got first string as title
    
        TextView tvSnippet = (TextView) popup.findViewById(R.id.snippet) ;
        tvSnippet.setText(marker.getSnippet());
    
        TextView tvSnippet2 = (TextView) popup.findViewById(R.id.snippet_2) ;
        tvSnippet2.setText(str2[1]);// got second string
    
        return popup;
    }