【问题标题】:How to add objects in an InfoWindow making it larger如何在信息窗口中添加对象使其变大
【发布时间】:2018-12-11 12:28:42
【问题描述】:


我需要在标记的InfoWindow 中有一个Button 和一个ProgressBar。我有两个主要问题是:

  1. InfoWindow 不够大,无法包含我需要的文本。
  2. 我不知道如何在InfoWindow 中添加任何类型的对象。

这是它在我的地图中的样子以及我用来生成标记的代码:

MarkerOptions markerOptions = new MarkerOptions().position(enemy.position).title("Mostro " + enemy.nome).snippet("Livello " + enemy.level + "\n" + enemy.health());
map.addMarker(markerOptions);

如您所见,我在 sn-p 中写的其余文本未显示。

如何解决这两个问题?
提前感谢您的耐心和考虑。

【问题讨论】:

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


    【解决方案1】:

    为信息窗口创建一个自定义布局,如下所示,并使用此布局初始化您的 infowindowadapter:

    ********信息窗口********

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:orientation="vertical"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
    
        <RelativeLayout
                android:layout_gravity="center"
                android:layout_width="300dp"
                android:layout_height="wrap_content">
    
            <TextView
                    android:id="@+id/location_tv"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="16dp"
                    android:gravity="center"
                    android:text="BluePearl Veterinary Partners"
                    android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
                    android:textColor="@color/about_color"
                    android:layout_centerHorizontal="true"/>
    
            <LinearLayout
                    android:layout_marginTop="16dp"
                    android:gravity="center_vertical"
                    android:layout_below="@+id/location_tv"
                    android:id="@+id/direction_layout"
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
    
    
    
                <ImageView
                        android:id="@+id/direction_imageview"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:scaleType="fitXY"
                        android:layout_marginStart="16dp"
                        android:src="@drawable/ic_place" />
    
                <TextView
                        android:id="@+id/address_tv"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="16dp"
                        android:layout_marginEnd="16dp"
                        android:text="1 West 15th Street, New York, NY, 10011"
                        android:layout_toRightOf="@+id/direction_imageview"
                        android:layout_below="@+id/location_tv"
                        android:textSize="13sp"
                />
    
    
            </LinearLayout>
    
            <LinearLayout
                    android:layout_marginTop="12dp"
                    android:gravity="center_vertical"
                    android:id="@+id/phone_layout"
                    android:layout_below="@+id/direction_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
    
                <ImageView
                        android:id="@+id/phone_imageview"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:layout_marginStart="16dp"
                        android:layout_alignParentLeft="true"
                        android:layout_below="@+id/direction_imageview"
                        android:src="@drawable/phone_red" />
    
                <TextView
                        android:id="@+id/ph_tv"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="16dp"
                        android:textSize="13sp"
                        android:text="212-924-3311"
                        android:layout_toRightOf="@+id/phone_imageview"
                        android:layout_below="@+id/address_tv" />
    
            </LinearLayout>
    
    
    
    
            <View
                    android:layout_below="@+id/phone_layout"
                    android:layout_width="match_parent"
                    android:layout_height="16dp"
                    android:layout_marginStart="16dp"
                    android:layout_marginEnd="16dp"/>
    
    
    
        </RelativeLayout>
    
    </LinearLayout>
    

    ********InfoWindow.kt********

    class InfoWindow(private val context: Context) : GoogleMap.InfoWindowAdapter {
    
        override fun getInfoWindow(marker: Marker): View? {
            return null
        }
        override fun getInfoContents(marker: Marker): View {
            val view = (context as Activity).layoutInflater
                .inflate(R.layout.info_window_map, null)
            val location_tv = view.findViewById<TextView>(R.id.location_tv)
            val address_tv = view.findViewById<TextView>(R.id.address_tv)
            val phone_tv = view.findViewById<TextView>(R.id.ph_tv)
            val directoryModel = marker.tag as HospitalsList?
            if (directoryModel != null) {
                location_tv.text = directoryModel.title
                address_tv.text = directoryModel.address
                phone_tv.text = directoryModel.contactnumber
            }
            return view
        }
    }
    

    ********MapActivity.kt********

    在您初始化地图片段的 MapActivity 中,使用以下代码行:

    val customInfoWindow = InfoWindow(this)
       map.setInfoWindowAdapter(customInfoWindow)
    

    【讨论】:

    • 非常感谢!只有一件事(也许是愚蠢的)我不明白:如何创建自定义布局?
    • 只需在 android studio 的 layout 文件夹下创建一个新的布局资源文件,然后粘贴我的答案中的 XML 代码并根据需要进行更改。如果它完全回答了你的问题,请接受我的回答。
    • 好的,现在我有了我想要的 InfoWindow,但它太大了。另一个问题是我无法单击该按钮,因为似乎我单击了信息窗口。我该如何解决?
    • 1) 您可以随时根据需要调整 infowindow.xml 的高度和宽度。 2)您可以使用自己的信息窗口单击侦听器来达到预期的效果。正如文档所说,您无法访问信息窗口中的视图。 Documentation 3)幸好有人写了一个库来解决这个问题,你可以看看Here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 2021-06-29
    • 1970-01-01
    • 2013-03-06
    • 1970-01-01
    • 2011-11-09
    相关资源
    最近更新 更多