android setOnInfoWindowClickListener - 传递更多值

时间:2018-05-04 16:38:03

标签: android google-maps onclicklistener

我在我的Android应用中使用了一个带有许多标记的Google地图。单击标记后,标题显示。但是我需要在点击该标题后打开一个包含更多值的新活动 - 这将显示在文本视图中。

我现在的解决方案现在有效,但每次打开相同的信息时 - 如果我点击任何标记,则传递最后一项的值。我需要每个标记传递它的值。

TCanvas

我还尝试将setOnInfoWindowClickListener放出for循环,但是不知道像place,info这样的变量。

2 个答案:

答案 0 :(得分:1)

首先,定义一个MarkerInfo类,它将保存每个Marker的所有信息:

public class MarkerInfo {
    public String mTitle;
    public String mPlace;
    public String mPlace2;
    public String mPerexfull;
    public String mImg1;
    public String mInfo;

    public MarkerInfo(String title, String place, String place2, String perexfull, String img1, String info) {
        mTitle = title;
        mPlace = place;
        mPlace2 = place2;
        mPerexfull = perexfull;
        mImg1 = img1;
        mInfo = info;
    }
}

然后,将HashMap定义为成员变量,将Marker作为键,将MarkerInfo对象作为值:

GoogleMap mMap;
Map<Marker, MarkerInfo> mMarkerMap = new HashMap<>();

然后,为每个Marker创建一个MarkerInfo对象,并将其添加到HashMap中。然后,当单击InfoWindow时,从相应的MarkerInfo对象中检索信息:

void createMarkersFromJson(String json) throws JSONException {

    JSONObject jsonObj = new JSONObject(json);
    JSONArray actors = jsonObj.getJSONArray("result");

    for (int i = 0; i < actors.length(); i++) {
        final JSONObject c = actors.getJSONObject(i);

        final double g1 = Double.parseDouble(c.getString("gps1"));
        final double g2 = Double.parseDouble(c.getString("gps2"));
        LatLng poss = new LatLng(g1,g2);

        final String title = c.getString("name");
        final String place = c.getString("place");
        final String place2 = c.getString("place2");
        final String perexfull = c.getString("perexfull");
        final String img1 = c.getString("img1");
        final String info = c.getString("info");

        Marker marker = mMap.addMarker(new MarkerOptions().position(poss).title(title).icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)));

        MarkerInfo markerInfo = new MarkerInfo(title, place, place2, perexfull, img1, info);

        mMarkerMap.put(marker, markerInfo);
    }

    //Set this only once:
    mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {

        @Override
        public void onInfoWindowClick(Marker marker) {
            MarkerInfo markerInfo = mMarkerMap.get(marker);


            Intent intent = new Intent(MapsActivity.this, SingleitemView.class);

            intent.putExtra("place", markerInfo.mPlace);
            intent.putExtra("place2", markerInfo.mPlace2);
            intent.putExtra("perexfull", markerInfo.mPerexfull);
            intent.putExtra("name", markerInfo.mTitle);
            intent.putExtra("img1", markerInfo.mImg1);
            intent.putExtra("info", markerInfo.mInfo);
            startActivity(intent);
        }
    });
}

答案 1 :(得分:0)

不确定我是否完全正确地完成了它,但这个解决方案终于适合我了!我将setOnInfoWindowClickListener从循环中取出然后:

    HashMap<Marker, String> values1 = new HashMap<Marker, String>();
    HashMap<Marker, String> values2 = new HashMap<Marker, String>();

 values1.put(marker, c.getString("place"));
 values1.put(marker, c.getString("place2"));

然后在onInfoWindowClick里面我用过:

   intent.putExtra("place", values1.get(marker));
            intent.putExtra("place2", values2.get(marker));