我在Android应用中使用带有很多标记的Google地图。单击标记后,将显示标题。但是我需要在单击该标题后打开一个具有更多值的新活动-该活动将显示在textview中。

我当前的解决方案现在可以使用,但是每次都显示相同的信息时会打开-如果单击任何标记,则传递上一项的值。我需要每个标记来传递它的值。

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

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

        mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {

            @Override
            public void onInfoWindowClick(Marker marker) {
                Intent intent = new Intent(MapsActivity.this, SingleitemView.class);

                intent.putExtra("place", place);
                intent.putExtra("place2", place2);
                intent.putExtra("perexfull", perexfull);
                intent.putExtra("name", title);
                intent.putExtra("img1", img1);
                intent.putExtra("info", info);
                startActivity(intent);
            }
        });

    }
      }


我还尝试将setOnInfoWindowClickListener置于for循环之外,但随后未知诸如place,info之类的变量。

最佳答案

首先,定义一个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);
        }
    });
}

08-04 09:03