2017-02-24 12 views
0

Hallo Ich habe eine Android-App mit einer Karte auf der Grundlage von Google Maps API, um Punkte von Interesse einer Stadt zu zeigen. Ich muss ein Info-Fenster hinzufügen: Wenn ich auf eine Markierung klicke, öffnet sich das Fenster und in diesem Fenster erscheint ein Bild und eine kurze Beschreibung. Ich lese einige Anleitungen, aber keine war hilfreich für mich. Danke.Wie füge ich ein Infowindow mit Text und Bild hinzu?

Antwort

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()); 
} 
Verwandte Themen