Android创建自定义onMarkerClickListener

时间:2016-12-26 12:25:34

标签: android onclick google-maps-markers

我正在尝试在自定义类上实现android OnMarkerClickListener,只要单击标记,该自定义类就会用作点击侦听器。

这就是我实施它的方式:

首先,我创建一个实现GoogleMap.OnMarkerClickListener的自定义类,如下所示:

    public class CustomMarkerClick extends MapsActivity implements GoogleMap.OnMarkerClickListener {

    private String mHaoTitle, mHaoDescription;
    private Context context;


    public CustomMarkerClick(Context context, String haoTitle, String haoDescription) {
        this.context = context;
        this.mHaoTitle = haoTitle;
        this.mHaoDescription = haoDescription;

        //I used this to check whether the values are being supplied to the constructor successfully
        Toast.makeText(context, haoTitle, Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        LinearLayout bottomSheetViewgroup = (LinearLayout) ((Activity)context).findViewById(R.id.design_bottom_sheet_hao);
        bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetViewgroup);
        bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED );

        TextView tv = (TextView) ((Activity)context).findViewById(R.id.txt_hao_title);
        tv.setText(mHaoTitle);
        return false;
    }
}

这是在点击标记时:

 mMap.setOnMarkerClickListener(new CustomMarkerClick(MapsActivity.this, bsHaoName, haoRetriever.getDescription()));

问题在于,每当我在setText方法的TextView上尝试onMarkerClick时,文字实际上都不是可见。例如,通过构造函数提供的haoTitle字符串在onMarkerCLick方法

中不可见

1 个答案:

答案 0 :(得分:0)

我在代码中使用了infoWindowAdapter。这是如下..

    mMap.setInfoWindowAdapter(new InfoWindowAdapter() {
            @Override
            public View getInfoWindow(Marker marker) {
                View v = mapActivity.getLayoutInflater().inflate(
                        R.layout.info_window_layout, null);

                ((TextView) v).setText(marker.getTitle());
                return v;
            }

            @Override
            public View getInfoContents(Marker marker) {

                // Getting view from the layout file info_window_layout View

                // Getting reference to the TextView to set title TextView

                // Returning the view containing InfoWindow contents return
                return null;

            }

        });

        mMap.setOnMarkerClickListener(new OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                marker.showInfoWindow();
                return true;
            }
        }); 

希望这有帮助!