Mariusz
Mariusz

Reputation: 1410

Maps Markers show same Info

i got Google Map with some Markers on it but all Markers have shame info how to make different info for different marker?

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

Upvotes: 0

Views: 888

Answers (2)

Cpt.Ohlund
Cpt.Ohlund

Reputation: 2679

The reason all markers has the same info is because you declared Station st = station as final.

Instead set the information you wish to show as a property of the marker, then you can access it when getInfoContents(..) is called.

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

Upvotes: 2

Biraj Zalavadia
Biraj Zalavadia

Reputation: 28484

Do this way

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

Upvotes: 0

Related Questions