2017-02-24 60 views
0

嗨我有一個基於谷歌地圖API顯示一個城市的興趣點的地圖的Android應用程序。我需要添加一個信息窗口:當我點擊一個標記時,窗口會打開,並在此窗口中顯示圖像和簡要描述。 我閱讀了一些指南,但任何對我沒有幫助。 謝謝。如何在文本和圖像中添加infowindow?

回答

0
/** 
    * Create a custom info window for Google maps to show the button. Ref: 
    * https://developers.google.com/maps/documentation/android-api/infowindows 
    */ 
    private class CustomInfoWindowAdapter implements InfoWindowAdapter { 

     private final View mCustomView; 

     CustomInfoWindowAdapter() { 
      mCustomView = getActivity() 
        .getLayoutInflater() 
        .inflate(R.layout.travel_tools_custom_map_info_window, null); 
     } 

     /** 
     * This method allow us to provide a view that will be used for the entire info window. 
     */ 
     @Override 
     public View getInfoWindow(Marker marker) { 
      //In this case we are inflating a custom view with all views already setup, so we don't 
      // need to do anything else. 
      return null; 
     } 

     /** 
     * This method allow us to just customize the contents of the window. 
     */ 
     @Override 
     public View getInfoContents(Marker marker) { 
      TextView tvTitle = ((TextView) mCustomView.findViewById(R.id.title)); 
      tvTitle.setText(marker.getTitle()); 

      return mCustomView; 
     } 
    } 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:clipToPadding="false" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="?android:attr/textColorPrimary" 
     android:textStyle="bold"/> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="8dp" 
     android:layout_marginEnd="8dp" 
     android:layout_marginStart="8dp" 
     android:layout_marginTop="8dp"/> 
</LinearLayout> 

@Override 
    public void onMapReady(GoogleMap googleMap) { 
     map.setInfoWindowAdapter(new CustomInfoWindowAdapter()); 
} 
相關問題