0

いくつかのマーカーが付いた Google マップを取得しましたが、すべてのマーカーには恥ずべき情報があり、マーカーごとに異なる情報を作成する方法を教えてください。

for (Station station : stationsListResponse.data.stations) {
            final Station st = station;
            map.addMarker(new MarkerOptions().position(new LatLng(station.getLatitude(), station.getLongitude())));
            map.setInfoWindowAdapter(new InfoWindowAdapter() {

                @Override
                public View getInfoWindow(Marker arg0) {
                    return null;
                }

                @Override
                public View getInfoContents(Marker marker) {

                    View v = getLayoutInflater().inflate(R.layout.info_window, null);
                    TextView info= (TextView) v.findViewById(R.id.info);
                    info.setText(st.street+"\n"+st.city);
                    return v;
                }
            });
        }
4

2 に答える 2

2

すべてのマーカーの情報が同じである理由は、Station st = station を final として宣言したためです。

代わりに、表示する情報をマーカーのプロパティとして設定すると、getInfoContents(..) が呼び出されたときにアクセスできます。

        for (Station station : stationsListResponse.data.stations) 
        {
            map.addMarker(new MarkerOptions().position(new LatLng(station.getLatitude(), station.getLongitude())).snippet(station.street+"\n"+station.city));
        }

        map.setInfoWindowAdapter(new InfoWindowAdapter() {

            @Override
            public View getInfoWindow(Marker arg0) {
                return null;
            }

            @Override
            public View getInfoContents(Marker marker) {

                View v = getLayoutInflater().inflate(R.layout.info_window, null);
                TextView info= (TextView) v.findViewById(R.id.info);
                info.setText(marker.getSnippet());
                return v;
            }
        });
于 2013-09-26T12:23:09.047 に答える
0

こうすれば

map.setInfoWindowAdapter(new InfoWindowAdapter() {

        @Override
        public View getInfoContents(Marker marker) {
            return null;
        }

        @Override
        public View getInfoWindow(Marker marker) {
              View v = getLayoutInflater().inflate(R.layout.info_window, null);
              TextView info= (TextView) v.findViewById(R.id.info);
              info.setText(st.street+"\n"+st.city);
              return v;
        }
    });
于 2013-09-26T12:15:21.163 に答える