【问题标题】:Maps Markers show same Info地图标记显示相同的信息
【发布时间】:2013-09-26 20:23:09
【问题描述】:

我得到了带有一些标记的谷歌地图,但所有标记都有羞耻信息如何为不同的标记制作不同的信息?

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

【问题讨论】:

    标签: android google-maps-markers google-maps-android-api-2


    【解决方案1】:

    所有标记具有相同信息的原因是您将 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;
                }
            });
    

    【讨论】:

      【解决方案2】:

      这样做

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

      【讨论】:

      • 更糟糕的是,所有人都有相同的信息,现在都失去了背景:(
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-08
      • 2011-02-24
      • 2012-08-28
      • 2019-05-17
      • 2017-03-15
      • 2014-10-15
      相关资源
      最近更新 更多