Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

android - How can I display more than just the Title and Snippet in a Google Maps Custom Info Window?

I'm having a hard time dealing with info window of google maps. Everything goes fine until I try to add info in a loop! I'm just not able to add info in a Loop and display it in the Info Window. This is a custom info window which I inflate from a layout xml file. I have successfully managed to add multiple markers in the loop(I receive jsonarray with information and add markers in the loop with received longitude and latitude). the problem is that Google maps takes only 2 kinds of strings which are the Title and the Snippet, but I have 5 pieces of information blogs to put in the info window,so how can I do it? By default I cant add image to infowindow thats the purpose for me to create my custom info window. (I'll post code of a loop and of an info window)

public void addpaqs(){
try {
    JSONArray array =  new JSONArray(paqsresponse);

    for (int i = 0; i < array.length(); i++) {
        JSONObject row = array.getJSONObject(i);
        tolongitude = row.getString("to_longitude");
        tolatitude = row.getString("to_latitude");
        creatorfirstname = row.getString("creator_first_name");
        creatorlastname = row.getString("creator_last_name");
        paqtype = row.getString("paq_type");
        startdate = row.getString("start_date");
        enddate=row.getString("end_date");
        fromplace = row.getString("from_country");
        toplace = row.getString("to_country");
        fromcity =row.getString("from_city");
        tocity = row.getString("to_city");
        String price = row.getString("price");
        double tolongdouble = Double.parseDouble(tolongitude);
        double tolatdouble = Double.parseDouble(tolatitude);
        MarkerOptions options = new MarkerOptions();
        options.position(new LatLng(tolatdouble, tolongdouble));
        options.Price(price);
        options.icon(BitmapDescriptorFactory.fromResource(R.drawable.paqqyinactive));
        options.snippet(creatorfirstname+creatorlastname);
        map2.addMarker(options);

    }
    } catch (JSONException e) {
        e.printStackTrace();
    }

}

inside the loop with method of addpaqs() and now my custom infowindow

 addpaqs();
            map2.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
                @Override
                public View getInfoWindow(Marker marker) {
                    return null;
                }

                @Override
                public View getInfoContents(Marker marker) {
                    // Setting up the infoWindow with current's marker info
                    //infoSnippet.setText(marker.getSnippet());
                    infoButtonListener.setMarker(marker);
                    firstnamelastname.setText(marker.getPrice());
                    Log.d("firstnamelastname",marker.getPrice().toString());

                    // We must call this to set the current marker and infoWindow references
                    // to the MapWrapperLayout
                    mapWrapperLayout.setMarkerWithInfoWindow(marker, infoWindow);
                    return infoWindow;
                }
            });
enter code here
enter code here

here i call addpaqs,and later i just dont know what to do with it!:( EDIT: my custom info window(how do i set up it)

 private ViewGroup infoWindow;     
this.infoWindow = (ViewGroup) getLayoutInflater().inflate(R.layout.infowindow, null);
        this.firstnamelastname = (TextView) infoWindow.findViewById(R.id.firstnamelastname);
        this.infoButton = (Button) infoWindow.findViewById(R.id.button);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...