【问题标题】:Google Map with Layout layer above it上面有布局层的谷歌地图
【发布时间】:2018-09-04 11:00:18
【问题描述】:

我在谷歌地图上方添加了一个布局,我自己的布局具有透明背景。现在,当我添加它时,我无法在我的应用程序中移动谷歌地图,因为它上面有一个框架布局。我的要求是我希望我的谷歌地图能够像滑动缩放一样工作,即使我上面有 Framelayout 也是如此。

下面是示例代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.googlemapactivity.GoogleMapMainActivity" />

    <FrameLayout
        android:id="@+id/frameTop"
        android:layout_above="@+id/llBottomButtom"
        android:layout_width="match_parent"
        android:descendantFocusability="beforeDescendants"
        android:layout_height="match_parent"/>

    <LinearLayout
        android:id="@+id/llBottomButtom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:weightSum="90">

        <Button
            android:id="@+id/btnOne"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="Route" />

        <Button
            android:id="@+id/btnTwo"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="Current" />

        <Button
            android:id="@+id/btnThree"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="Charge" />


    </LinearLayout>


</RelativeLayout>

这里支持地图片段不滑动移动什么的。有相同的解决方案吗?

【问题讨论】:

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


    【解决方案1】:

    试试这个:

    问题是您尝试在 id 声明上方添加 android:layout_above="@+id/llBottomButtom" 行,将其添加到 LinearLayout 下方,以便对 FrameLayout 有用。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    
    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.googlemapactivity.GoogleMapMainActivity" />
    
    
    <LinearLayout
        android:id="@+id/llBottomButtom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:weightSum="90">
    
        <Button
            android:id="@+id/btnOne"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="Route" />
    
        <Button
            android:id="@+id/btnTwo"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="Current" />
    
        <Button
            android:id="@+id/btnThree"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="Charge" />
    
    
    </LinearLayout>
    
    <FrameLayout
        android:id="@+id/frameTop"
        android:layout_above="@+id/llBottomButtom"
        android:layout_width="match_parent"
        android:descendantFocusability="beforeDescendants"
        android:layout_height="wrap_content"/>
    

    【讨论】:

    • 一旦 Framelayout 将任何 Fragment 加载到其容器中,Map 就会停止工作。
    • 你能解释一下为什么需要 frameTop FrameLayout 吗?
    • 问题是我正在添加一个布局以在地图上显示一些额外的 UI 组件,因为我正在添加一个布局。
    • 是因为你的FrameLayout覆盖了地图片段,我上面的代码试过了吗?正如你给 FrameLayout 的 match_parent 高度一样,试试 wrap_content。
    • 我得到了答案,添加它。
    【解决方案2】:

    所以我对谷歌地图做了一些文档阅读,发现地图提供了 cameraAnimation 方法,我们可以在其中自己添加滚动,所以我所做的是实现了 GestureDetector.OnGestureListener ,其中有两个方法我按照下面提供的方式使用

      override fun onFling(e1: MotionEvent?, e2: MotionEvent?, velocityX: Float, velocityY: Float): Boolean {
            googMaps?.animateCamera(CameraUpdateFactory.scrollBy(velocityX, velocityY))
            return true
        }
    
        override fun onScroll(e1: MotionEvent?, e2: MotionEvent?, distanceX: Float, distanceY: Float): Boolean {
            googMaps?.animateCamera(CameraUpdateFactory.scrollBy(distanceX, distanceY))
            return true
        }
    

    这成功了,现在我可以在我的地图上方使用带有片段的自定义布局,它仍然可以滚动并且功能相同。

    【讨论】:

      猜你喜欢
      • 2014-05-18
      • 1970-01-01
      • 2019-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多