如何添加文本和图像的infowindow?

时间:2017-02-24 15:20:10

标签: maps google-maps-android-api-2

嗨我有一个Android应用程序,带有基于谷歌地图api的地图,以显示一个城市的兴趣点。我需要添加一个信息窗口:当我点击一个标记时,窗口会打开,在这个窗口中会显示一个图像和一个简短的描述。 我读了一些指南,但任何对我都没有帮助。 感谢。

1 个答案:

答案 0 :(得分: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());
}