【发布时间】:2020-06-13 14:47:56
【问题描述】:
我想将View 放置在 GoogleMap 对象的某个位置/区域。
它必须是 View,而不仅仅是 Marker,因为我想向它添加一个 OnDragListener。
有another question with a similar title,但我认为它不适用于我的用例。
我试图获取MapView 上触摸的 x 和 y 位置,以便确定放置 View 的位置,但 GoogleMap 对象消耗了MapView 的触摸事件。
在我的布局中,我放置了一个内部ConstraintLayout,ID 为con_red_rectangle,另一个包含MapView。想法是将 con_red_rectangle 放置在地图的某个位置(截图如下)。
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/con_top"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/con_red_rectangle"
android:layout_width="200dp"
android:layout_height="50dp"
android:background="@drawable/rounded_red"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:translationZ="1dp">
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/con_map_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:translationZ="0dp">
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
在onMapReady回调中,我动态设置了con_red_rectangle的边距。但是,它不是在地图上定位,而是基于父布局。
val lp: ConstraintLayout.LayoutParams = con_red_rectangle?.layoutParams as ConstraintLayout.LayoutParams
lp.leftMargin=x
lp.topMargin=y
con_red_rectangle!!.layoutParams=lp
截图
我意识到我的问题可能不太清楚,所以希望截图能有所帮助。
我想在名为“Place View Here”的Marker 所在的位置放置一个View。 View 将覆盖地图的某个区域,并且不会随着相机的移动而“移动”。我也意识到缩放变化期间的大小可能会成为问题,但首要任务是在该位置放置 View。
如屏幕截图所示,红色半透明View 是根据主布局而非地图定位的。
【问题讨论】:
标签: android xml google-maps kotlin