本文介绍了如何将自定义InfoWindow for Android中的getInfoContents传递给title和片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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设置我的两个infowindow文本视图的文本)和 marker.getSnippet()在 getInfoContents()方法中:

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

返回弹出;
}

这对于前两个文字浏览来说都很好,但我想知道的是什么正确的方式让我传递第三个字符串 infoWindowContents()用于 tvSnippet2 ?显然,我不能使用 .title() /。 snippet()和标记。 getTitle() / marker.getSnippet()因为这些已经被使用并且会重复数据。

infoWindowAdapter:



 公共类InfoWindow实现InfoWindowAdapter {

private查看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);

返回弹出;
}

@Override
public View getInfoWindow(Marker marker){

return null;
}



}


解决方案

当时在 Map 中添加 Marker concat 您的标题字符串中的所有字符串像

  String title =First String+_+Second String; 

然后将它添加到您的标题

  all = mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory
.fromResource(R.drawable (标题)
.snippet(片段);

现在,当您点击任何标记时,您的 CustomInfoWindow 上升。因此,您将在 getInfoContents(...)中解析您的标题,如

 <$ c $ b $ @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]); //获得第一个字符串作为标题

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]); //得到第二个字符串

return popup;
}


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



}
解决方案

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

这篇关于如何将自定义InfoWindow for Android中的getInfoContents传递给title和片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 03:35