android - 单击标记-> getInfoContents 不显示我的自定义小部件
问题描述
安卓工作室:
实施 'com.google.android.gms:play-services-maps:17.0.0'
在我的活动中:
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
private lateinit var mMap: GoogleMap
mMap.setInfoWindowAdapter(object : GoogleMap.InfoWindowAdapter {
override fun getInfoWindow(marker: Marker): View? {
return null
}
// Important! Not work with "androidx.constraintlayout.widget.ConstraintLayout"
override fun getInfoContents(marker: Marker): View? {
val markerLatLng = marker.position
val selectGazStation =
gazStationsList.first { it.latitude == markerLatLng.latitude && it.longitude == markerLatLng.longitude }
Debug.d(TAG, "getInfoContents: selectGazStation = $selectGazStation")
val agentSummaryInfoWidget = AgentSummaryInfoWidget(application, selectGazStation)
return agentSummaryInfoWidget
}
})
}
这是我的自定义小部件AgentSummaryInfoWidget
class AgentSummaryInfoWidget(context: Context, selectGazStation: GazStation) :
LinearLayout(context) {
init {
init(context, selectGazStation)
}
companion object {
private val TAG = AgentSummaryInfoWidget::class.java.name
}
private fun init(context: Context, selectGazStation: GazStation) {
val mapMarkerInfoContentLayoutBinding =
MapMarkerInfoContentLayoutBinding.inflate(LayoutInflater.from(context));
mapMarkerInfoContentLayoutBinding.addressValueTextView.setText(selectGazStation.address)
var workingHours = "24/24"
if (!selectGazStation.workingHours?.contains("NON-STOP", true)!!) {
workingHours = selectGazStation.workingHours!!
}
mapMarkerInfoContentLayoutBinding.workingHoursValueTextView.setText(workingHours)
mapMarkerInfoContentLayoutBinding.phoneValueTextView.setText(selectGazStation.phone)
}
}
这里是 xml map_marker_info_content_layout
:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/titleContainerLayout"
android:layout_width="match_parent"
android:layout_height="@dimen/min_height"
android:layout_marginStart="@dimen/half_default_margin"
android:layout_marginTop="@dimen/default_margin"
android:layout_marginEnd="@dimen/half_default_margin"
android:background="@drawable/bottom_border_bg"
android:orientation="vertical">
<TextView
android:id="@+id/titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/half_default_margin"
android:text="Rompetrol"
android:textColor="@android:color/black"
android:textSize="15sp" />
<TextView
android:id="@+id/stateTtextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/half_default_margin"
android:text="@string/open"
android:textColor="@android:color/holo_green_light"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent" />
</LinearLayout>
<LinearLayout
android:id="@+id/agentInfoContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bottom_border_bg"
android:orientation="vertical">
<LinearLayout
android:id="@+id/containerAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/half_default_margin"
android:layout_marginTop="@dimen/half_default_margin"
android:layout_marginEnd="@dimen/half_default_margin">
<TextView
android:id="@+id/addressLabelTextView"
style="@style/mapMarkerInfoItemLabelTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/half_default_margin"
android:layout_marginEnd="@dimen/half_default_margin"
android:text="@string/address_colon" />
<TextView
android:id="@+id/addressValueTextView"
style="@style/mapMarkerInfoItemLabelTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/half_default_margin"
android:gravity="end" />
</LinearLayout>
<LinearLayout
android:id="@+id/containerWorkingHours"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/half_default_margin"
android:layout_marginTop="@dimen/half_default_margin"
android:layout_marginEnd="@dimen/half_default_margin">
<TextView
android:id="@+id/workingHoursLabelTextView"
style="@style/mapMarkerInfoItemLabelTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/half_default_margin"
android:layout_marginEnd="@dimen/half_default_margin"
android:text="@string/working_hours_colon" />
<TextView
android:id="@+id/workingHoursValueTextView"
style="@style/mapMarkerInfoItemLabelTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/half_default_margin"
android:gravity="end"
app:layout_constraintEnd_toEndOf="parent" />
</LinearLayout>
<LinearLayout
android:id="@+id/containerPhon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/half_default_margin"
android:layout_marginTop="@dimen/half_default_margin"
android:layout_marginEnd="@dimen/half_default_margin"
android:layout_marginBottom="@dimen/half_default_margin">
<TextView
android:id="@+id/phoneLabelTextView"
style="@style/mapMarkerInfoItemLabelTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/half_default_margin"
android:layout_marginEnd="@dimen/half_default_margin"
android:text="@string/phone_colon" />
<TextView
android:id="@+id/phoneValueTextView"
style="@style/mapMarkerInfoItemLabelTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/half_default_margin"
android:gravity="end" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/containerServices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/half_default_margin"
android:layout_marginTop="@dimen/half_default_margin"
android:layout_marginEnd="@dimen/half_default_margin"
android:layout_marginBottom="@dimen/half_default_margin"
android:orientation="vertical">
<TextView
android:id="@+id/servicesLabelTextView"
style="@style/mapMarkerInfoItemLabelTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/half_default_margin"
android:layout_marginEnd="@dimen/half_default_margin"
android:text="@string/services_colon" />
<LinearLayout
android:id="@+id/gazStationsServicesContainer"
android:layout_width="match_parent"
android:layout_height="64dp"
android:gravity="start|center"
android:orientation="horizontal">
<com.yarolegovich.discretescrollview.DiscreteScrollView
android:id="@+id/gazStationServiceDiscreteScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:dsv_orientation="horizontal" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</layout>
当我点击市场时,我的自定义小部件不会显示。这里的结果:
解决方案
您不能直接在标记上浮动自定义视图,您可以通过使用自定义标记(包含自定义布局上方的位置图标)并在地图上使用此布局并编写 onclick 侦听器来显示和隐藏
推荐阅读
- mysql - 未绑定命名参数:Spring Boot 中的 DATE_FORMAT 本机查询
- json - 带有“oneOf”的json模式中的嵌套对象
- python - 如何在 forloop 中创建多个数据框?
- airflow - 在 Airflow DAG python 文件中读取存储在 hdfs 中的文件
- c# - 将数据从 form2 方法传递到 form1 文本框
- javascript - 邮递员检查数组数组中的哈希值
- android - Android:使锚链接在 ScrollView 内的 WebView 中工作
- python - 我们如何使用 Boto3 获取 IAM 服务上次访问的详细信息?
- tsql - SQL:在分区上使用 LEAD/LAG 和/或 ROW_NUMBER 查找 2 个事件之间的持续时间
- python - 如何拆分列表中的元素并与python中的另一个列表结合