我正在尝试在自定义类上实现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
方法
答案 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;
}
});
希望这有帮助!