如何在自定义InfoWindow for Android中将标题和片段传递给getInfoContents

时间:2015-01-30 10:56:37

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

我的Android应用中有一个InfoWindowAdapter类,它引用包含三个TextView的xml布局。我使用以下代码在addMarker()方法中添加了一个新标记:

mapView.addMarker(new MarkerOptions()
            .position(latLon)
            .title(titleText)
            .snippet(snippetText)
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_green)));

    mapView.setInfoWindowAdapter(new InfoWindow(getLayoutInflater()));
    mapView.setOnInfoWindowClickListener(this);

然后,我在marker.getTitle()方法中使用marker.getSnippet()getInfoContents()设置了我的两个infowindow文本视图的文本:

@Override
    public View getInfoContents(Marker marker) {

        if (popup == null) {

            popup = inflater.inflate(R.layout.infowindow_popup, null);
        }

        TextView tvTitle = (TextView) popup.findViewById(R.id.title);
        tvTitle.setText(marker.getTitle());

        TextView tvSnippet = (TextView) popup.findViewById(R.id.snippet) ;
        tvSnippet.setText(marker.getSnippet());

        TextView tvSnippet2 = (TextView) popup.findViewById(R.id.snippet_2) ;
        tvSnippet2.setText("test");

        return popup;
    }

这对前两个文本视图都有好处,但我想知道的是,我将第三个字符串传递给infoWindowContents()tvSnippet2一起使用的正确方法是什么?显然,我无法使用.title() /。snippet()marker.getTitle() / marker.getSnippet(),因为这些已被使用并会重复数据。

infoWindowAdapter:

public class InfoWindow implements InfoWindowAdapter {

    private View popup = null;
    private LayoutInflater inflater = null;
    String str;

    InfoWindow(LayoutInflater inflater, String s) {

        this.inflater = inflater;

        str = s;
    }

    @Override
    public View getInfoContents(Marker marker) {

        if (popup == null) {

            popup = inflater.inflate(R.layout.infowindow_popup, null);
        }

        TextView tvTitle = (TextView) popup.findViewById(R.id.title);
        tvTitle.setText(marker.getTitle());

        TextView tvSnippet = (TextView) popup.findViewById(R.id.snippet) ;
        tvSnippet.setText(marker.getSnippet());

        TextView tvSnippet2 = (TextView) popup.findViewById(R.id.snippet_2) ;
        tvSnippet2.setText(str);

        return popup;
    }

    @Override
    public View getInfoWindow(Marker marker) {

        return null;
    }



}

2 个答案:

答案 0 :(得分:4)

当你在Marker中添加Map concat 时,标题字符串中的所有Strings就像

String title="First String"+"_"+"Second String";

然后将其添加到您的title

all = mMap.addMarker(new MarkerOptions()
    .icon(BitmapDescriptorFactory
    .fromResource(R.drawable.mark_red))
    .position(Location)
    .title(title)
    .snippet(snippet);

现在,当您点击任意Marker时,您的CustomInfoWindow就会上升。所以您将在getInfoContents(...)中解析您的标题

 @Override
 public View getInfoContents(Marker marker) {

    if (popup == null) {

        popup = inflater.inflate(R.layout.infowindow_popup, null);
    }

    String str=marker.getTitle();
    final String[] str2=str.split("_");

    TextView tvTitle = (TextView) popup.findViewById(R.id.title);
    tvTitle.setText(str2[0]);// got first string as title

    TextView tvSnippet = (TextView) popup.findViewById(R.id.snippet) ;
    tvSnippet.setText(marker.getSnippet());

    TextView tvSnippet2 = (TextView) popup.findViewById(R.id.snippet_2) ;
    tvSnippet2.setText(str2[1]);// got second string

    return popup;
}

答案 1 :(得分:1)

有用于额外数据的标签这样的东西 Tag An Object associated with the marker. For example, the Object can contain data about what the marker represents. This is easier than storing a separate Map<Marker, Object>. As another example, you can associate a String ID corresponding to the ID from a data set. Google Maps Android API neither reads nor writes this property except that when a marker is removed from the map, this property is set to null.

//creating marker
EventEntity entity; // custom object full of data
Marker m = map.addMarker(new MarkerOptions().position(
                  new LatLng(entity.getLocation().getLat(), entity.getLocation().getLng()))
                  .title(entity.getEventName())

                  .icon(BitmapDescriptorFactory.fromResource(R.drawable.map_marker_copy)));
m.setTag(entity); // here we set our custom data as tag


  @Override public View getInfoContents(Marker marker) {

    EventEntity event = (EventEntity) marker.getTag();
    // and here we got EventEntity back so we can get all required data
    View v = LayoutInflater.from(getApplicationContext())
        .inflate(R.layout.infowindow_map, mapview, false);
    MyTextView tvTitle = (MyTextView) v.findViewById(R.id.tv_title);
    MyTextView tvDate = (MyTextView) v.findViewById(R.id.tv_date);
    MyTextView tvType = (MyTextView) v.findViewById(R.id.tv_type);
    MyTextView tvPrice = (MyTextView) v.findViewById(R.id.tv_price);

    tvTitle.setText(event.getEventName());
    tvType.setText(event.getType() + " event");
    tvPrice.setText(event.getPrice() + event.getCurrency());
    String date = dfDate.format(new Date(event.getDateStart()));
    String start = dfTime.format(new Date(event.getDateStart()));
    String end = dfTime.format(new Date(event.getDateEnd()));
    tvDate.setText(date + " " + start + "-" + end);
    return v;
  }