【问题标题】:Android: GoogleMaps marker details fragmentAndroid:GoogleMaps 标记详细信息片段
【发布时间】:2021-08-20 11:53:06
【问题描述】:

我是 Android 的初学者,我正在尝试实现类似于 Google Maps App 的功能。当我按下标记时,我希望在我的SupportMapFragment 上显示一个带有详细信息的新片段,如下所示:

我怎样才能做到这一点?

这是该片段的默认布局吗?

那是一个片段还是只是一个修改过的信息窗口?

【问题讨论】:

  • 这很可能是一个隐藏的视图,它变得可见并加载了所选信息。
  • @che10,谢谢。我想你是对的。我必须创建一个自定义视图。

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


【解决方案1】:

是的,您可以使用这个库轻松实现它,它在最小化和拉伸时支持 2 种布局,我在谷歌地图应用程序之后实现了类似的行为,获取如下数据并将其设置在布局中

here is link

样本用法是

    <com.sothree.slidinguppanel.SlidingUpPanelLayout
    xmlns:sothree="http://schemas.android.com/apk/res-auto"
    android:id="@+id/sliding_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom"
    sothree:umanoPanelHeight="68dp"
    sothree:umanoShadowHeight="4dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Main Content"
        android:textSize="16sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center|top"
        android:text="The Awesome Sliding Up Panel"
        android:textSize="16sp" />
</com.sothree.slidinguppanel.SlidingUpPanelLayout>

【讨论】:

    【解决方案2】:

    我想通了。我需要的是一个底页对话框。这是我用作灵感来源的非常 useful guide

    步骤 1
    在 `app.gradle` 中实现材质组件库:
    implementation 'com.google.android.material:material:1.3.0'
    
    步骤 2
    在 res->layout 中创建您选择的布局。我的看起来像这样:
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_gravity="bottom">
       
            <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:text="Rent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    

    布局与底部对齐。

    第三步
    定义一个实例化 BottomSheetDialog 的方法:
    private void showBottomSheetDialog() {
          final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(getContext());
          bottomSheetDialog.setContentView(R.layout.MY_LAYOUT_NAME);
          AppCompatButton btn = bottomSheetDialog.findViewById(R.id.btn_view);
          bottomSheetDialog.show();
       }
    
    第四步
    这一步是额外的,如果您想查看如何通过按下 GoogleMap 标记来调用 BottomSheetDialog。

    确保处理 MapSupportFragment 的类实现 OnMapReadyCallbackGoogleMap.OnMarkerClickListener 和(如果您需要标记集群)ClusterManager.OnClusterItemClickListener。覆盖必要的方法:

     @Override
       public boolean onMarkerClick(Marker marker) {
          showBottomSheetDialog();
          return false;
       } 
    

    如果你需要集群,不要忘记在@onMapReady() 中添加:

     map.setOnCameraIdleListener(clusterManager);
     map.setOnMarkerClickListener(clusterManager);
     clusterManager.setOnClusterItemClickListener(this);
    

    瞧!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-23
      • 1970-01-01
      • 1970-01-01
      • 2019-01-06
      • 2015-05-13
      • 2020-12-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多