How to pass more than just title and snippet to ge

2019-06-26 01:55发布

问题:

I have a InfoWindowAdapter class in my Android app that refers to an xml layout containing three TextViews. I add a new marker using the code below within an addMarker() method:

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

I then set the text of my two infowindow textviews using marker.getTitle() and marker.getSnippet() within a getInfoContents() method:

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

This is all good for the first two textviews but what I would like to know is what is the correct way for me to pass a third string to infoWindowContents() to use with tvSnippet2? Obviously I can't use .title()/.snippet() and marker.getTitle()/marker.getSnippet() because these are already used and would repeat the data.

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



}

回答1:

When you add Marker in Map at that time concat your all Strings in your Title string like

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

then add this to your title

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

Now, When you click into any Marker then your CustomInfoWindow rising up.So you'll parse your title in getInfoContents(...) like

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


回答2:

There is such thing as tag which is used for extra data 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;
  }