2013年5月9日 星期四

Android程式設計 - Google Maps API v2自訂地標訊息視窗

預設的地標訊息titlesnippet都只能顯示單行文字,當文字訊息過長時,後面的部份就只顯示「」而看不到完整的文字內容。
要自訂地標訊息視窗樣式,首先必須為地標訊息視窗設計所需的版面配置,例如使用一個圖示與二個文字方塊做為地標訊息視窗版面。



接著為GoogleMap設計一個Adapter類別,在該類別的getInfoContents()方法中,將所要顯示的地標titlesnippet內容設定到自訂的地標訊息視窗版面元件中。
class MyInfoWindowAdapter implements InfoWindowAdapter {
  @Override
  public View getInfoContents(Marker marker) {
     // 依指定layout檔,建立地標訊息視窗View物件
     View infoWindow = getLayoutInflater().inflate(R.layout.my_infowindow, null);
     // 顯示地標title
     TextView title = ((TextView)infoWindow.findViewById(R.id.txtTitle));
     title.setText(marker.getTitle());
     // 顯示地標snippet
    TextView snippet = ((TextView)infoWindow.findViewById(R.id.txtSnippet));
     snippet.setText(marker.getSnippet());
     return infoWindow;      
  }
  ...
}
最後設定GoogleMap使用自訂的MyInfoWindowAdapter物件做為地標訊息視窗轉接器。
    m_map.setInfoWindowAdapter(new MyInfoWindowAdapter());