【问题标题】:How to place a View on a certain map location of GoogleMap?如何在谷歌地图的某个地图位置放置一个视图?
【发布时间】: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 所在的位置放置一个ViewView 将覆盖地图的某个区域,并且不会随着相机的移动而“移动”。我也意识到缩放变化期间的大小可能会成为问题,但首要任务是在该位置放置 View

如屏幕截图所示,红色半透明View 是根据主布局而非地图定位的。

【问题讨论】:

    标签: android xml google-maps kotlin


    【解决方案1】:

    如果我正确理解您的问题,在这种情况下,您应该将视图放在 MapView 之后。约束布局取决于 XML 中的视图顺序。如果您的 MapView 低于它将被绘制在您的视图上。右视图顺序应如下所示:

    <!-- Top level layout. -->
    <ConstraintLayout>
    
        <!-- Layout with other views. -->
        <ConstraintLayout>
            <TextView/>
        </ConstraintLayout>
    
        <!-- Layout with a View (should be over the map) and the Google Map object. -->
        <ConstraintLayout>
            <MapView/>
            <View/> <!-- Referred to as "con_map_area" below. -->
        </ConstraintLayout>
    
    </ConstraintLayout>
    

    在这种情况下,View 首先消耗触摸事件。

    【讨论】:

      【解决方案2】:

      首先,ConstraintLayout 与其他布局不同,在其他视图之后声明的视图将出现在顶部,这与其他布局完全相反。

      这意味着在以下情况下:

      <ConstraintLayout>
          <View1/>
          View2/>
      </ConstraintLayout>
      

      View2 将位于 View1 之上,因为它是在 View1 之后声明的。与此相反的情况发生在相对或线性等布局中。

      这肯定行得通,您可以进一步将android:elevation 指定为5dp3dp(您的电话),以使视图保持在顶部并带有深度阴影。

      所以,像这样声明你的观点:

      <ConstraintLayout>
          <MapView/>
          <View/> <!-- Referred to as "con_map_area" below. -->        
      </ConstraintLayout>
      

      不要忘记您的第一个孩子ConstraintLayout 也将以这种方式工作,您可能希望在此之后声明。而且,最后你真的不需要嵌套ConstraintLayout,它是专为平面视图层次结构设计的。此外,要以编程方式更改位置,您应该使用ConstraintSet

      【讨论】:

      • 感谢您的回答和建议,Lalit Fauzdar。感谢您对优化布局的建议,但是,主要问题是我不知道如何将视图放置在 GoogleMap 对象之上。我可以通过设置不同的 translationZ 值将视图放置在地图上方,但我需要将视图放置在地图的特定区域。如果听起来令人困惑,我很抱歉。
      • 嗨@ErlendK.H。看看这两个:ThisThis。另外,我实际上不明白您想要什么,但是如果您想将视图放置在屏幕上的某个位置,您可以使用约束来完成,否则您想将其放置在地图上的某个位置然后检查第一个链接,
      • 感谢您的链接。我在问题中添加了屏幕截图,它有助于澄清我的问题吗?是的,我想在地图上的某个位置放置一个视图。至于第一个链接,也许它可以工作,但我认为这不是最佳解决方案,因为我想放置几个视图,使用自定义视图可能会大大影响性能。另外,我需要将视图作为变量。
      猜你喜欢
      • 2014-03-20
      • 1970-01-01
      • 2011-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-01
      • 2016-05-24
      相关资源
      最近更新 更多