如何在Google地图自定义信息窗口中显示的不仅仅是标题和代码段?

时间:2015-05-06 05:56:43

标签: android loops

我很难处理谷歌地图的信息窗口。 一切顺利,直到我尝试在循环中添加信息!我只是无法在循环中添加信息并在信息窗口中显示它。这是一个自定义信息窗口,我从布局xml文件中膨胀。我已成功设法在循环中添加多个标记(我接收带有信息的jsonarray并在循环中添加标记,并获得经度和纬度)。问题是谷歌地图只有2种字符串,即标题和片段,但我有5个信息博客放在信息窗口,所以我该怎么办呢?默认情况下,我无法将图像添加到infowindow,这是我创建自定义信息窗口的目的。 (我将发布循环和信息窗口的代码)

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();
    }

}

使用addpaqs()方法在循环内部 现在我的自定义信息窗口

 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

这里我打电话给addpaqs,后来我就不知道该怎么做了!:( 编辑: 我的自定义信息窗口(如何设置)

 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);

1 个答案:

答案 0 :(得分:3)

好的,我有一个简单的例子。这里的总体思路是使用包装类来存储每个Marker的数据,然后将数据存储在HashMap中,并将标记ID作为键,以便您可以在InfoWindowAdapter

首先,为每个Marker对应的信息创建一个持有者类(您可以根据需要对此进行扩展以包含更多信息):

    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;
        }
    }

然后创建一个HashMap<String, MarkerHolder>,将每个Marker ID映射到每个Marker的信息,并将其设为实例变量:

HashMap<String, MarkerHolder> markerHolderMap = new HashMap<String, MarkerHolder>();

以下是仅添加一个Marker的简化示例,请注意将信息添加到HashMap并将Marker ID作为关键字的位置:

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
}

以下是InfoWindow的自定义布局xml,您可以根据需要对其进行扩展:

<?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>

然后,把它们放在一起,这里是InfoWindowAdapter。请注意,我使用了Marker中存储的标题和摘要,但也使用了从MarkerHolder获得的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;

            }
        });

结果:

enter image description here