2016-12-26 117 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

我不認爲這將符合用戶要求,它必須是顯示信息的底部工作表,信息對話框是否可以修改以模仿底部工作表的外觀? –