基于Hiren Patel解决方案。此代码创建一个TextView
from布局,而不是从零开始。一个显着的区别是:如果您有集群,则在单击集群时不会看到空白标签。
override fun onMapReady(googleMap: GoogleMap) {
this.googleMap = googleMap
...
googleMap.setInfoWindowAdapter(object : GoogleMap.InfoWindowAdapter {
override fun getInfoContents(marker: Marker): View? {
return null
}
override fun getInfoWindow(marker: Marker): View? =
if (marker.title == null)
null
else {
val inflater = LayoutInflater.from(context)
val view = inflater.inflate(R.layout.layout_marker, null, false)
view.label.text = marker.title
view
}
})
layout_marker.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:textColor="$f0f0f0"
android:textSize="12sp"
tools:text="Marker"
android:background="#00aaee"
/>