2016-12-26 88 views
1

我试图在自定义类上实现android OnMarkerClickListener,当点击标记时它将被用作点击监听器。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())); 

问题是,无论何时我在onMarkerClick方法中尝试使用TextView上的setText时,文本都不是实际的ly visible。例如,通过构造函数提供的haoTitle字符串在onMarkerCLick方法中不可见

+0

你尝试过'infoWindowAdapter'吗? –

回答

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

希望这有助于!

+0

让我试试这个,一定会回复给你几个 –

+0

;祝你好运 –

+0

我不认为这将符合用户要求,它必须是显示信息的底部工作表,信息对话框是否可以修改以模仿底部工作表的外观? –