Android - GoogleMap - InfoWindow

Android - GoogleMap - InfoWindow

有時候,用原生的Marker的InfoWindow,不符合需求

所以需要做客製化的InfoWindow

因此需要準備,自己想要的InfoWindow

如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/tv_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="30sp"/>
    <TextView
        android:id="@+id/tv_gender"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="男"
        android:textSize="20sp"/>
    <TextView
        android:id="@+id/tv_age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="20歲"
        android:textSize="20sp"/>
</LinearLayout>

再建立一個

InfoWindowAdapter


public class MemberInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {

    private Activity context;

    public MemberInfoWindowAdapter (Activity context){
        this.context = context;
    }

    @Override
    public View getInfoWindow(Marker marker) {
        return null;
    }

    @Override
    public View getInfoContents(Marker marker) {
        View view = context.getLayoutInflater().inflate(R.layout.layout_infowindow_hotel, null);

        TextView tvName = (TextView) view.findViewById(R.id.tv_name);
        TextView tvGender = (TextView) view.findViewById(R.id.tv_gender);
        TextView tvAge = (TextView) view.findViewById(R.id.tv_age);

        String[] datas = marker.getSnippet().split(",");
        tvName.setText(marker.getTitle());

        if(datas.length>=2) {
            tvGender .setText(datas[0]);
            tvAge ice.setText(datas[1]);
        } else {

        }
        return view;
    }
}

這有點使用賤賤招的方式,因為我把想要的資訊,透過設定在Snippet,

再透過「,」切割,成陣列,再設定到各個值上

等到OnMapReady時,將客製化的InfoWindowAdapter設定到mMap去

        MemberInfoWindowAdapter adapter = new MemberInfoWindowAdapter (MapsActivity.this);
        mMap.setInfoWindowAdapter(adapter);