在Google Maps自定义信息窗口中,我不仅可以显示标题和摘录,还可以显示更多信息? [英] How can I display more than just the Title and Snippet in a Google Maps Custom Info Window?

查看:50
本文介绍了在Google Maps自定义信息窗口中,我不仅可以显示标题和摘录,还可以显示更多信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理Google地图的信息窗口时遇到了麻烦. 一切正常,直到我尝试循环添加信息!我只是无法在循环中添加信息并在信息"窗口中显示它.这是一个自定义信息窗口,我可以从布局xml文件中获取该信息.我已经成功地在循环中添加了多个标记(我收到了包含信息的jsonarray,并在循环中使用了接收到的经度和纬度添加了标记).问题是Google Maps仅采用Title和Snippet这两种字符串,但是我在信息窗口中放置了5条信息博客,该怎么办?默认情况下,我无法将图像添加到信息窗口,这是我创建自定义信息窗口的目的. (我将发布循环和信息窗口的代码)

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

}

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

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

在这里我叫addpaqs,后来我只是不知道该怎么做! 我的自定义信息窗口(如何设置)

here i call addpaqs,and later i just dont know what to do with it!:( 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);

推荐答案

好的,我得到了一个简单的示例.这里的总体思想是使用包装器类来存储每个Marker的数据,然后将以Marker ID为键的数据保存在HashMap中,以便可以在InfoWindowAdapter中获取它.

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.

首先,为每个Marker对应的信息创建一个holder类(您可​​以根据需要扩展它以包含更多信息):

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

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

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

这里是仅添加一个Marker的简化示例,请注意,其中信息是使用Marker ID作为键添加到HashMap的:

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
}

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

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>

然后,将它们放在一起,这是InfoWindowAdapter.请注意,我使用了存储在Marker中的标题"和代码段",但也使用了从MarkerHolder中获得的信息,该信息是从HashMap中获得的:

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;

            }
        });

结果:

这篇关于在Google Maps自定义信息窗口中,我不仅可以显示标题和摘录,还可以显示更多信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆