【问题标题】:Android GoogleMap v2 getting cropped or not fully displayedAndroid GoogleMap v2 被裁剪或未完全显示
【发布时间】:2016-07-20 19:26:59
【问题描述】:

我正在观察 Google 地图的意外行为。 问题是地图使用支持地图片段从顶部和底部区域进行裁剪, 如果我使用全屏,则地图完全可见,如图 1 所示,但如果我应用地图片段高度或重量属性,则地图不完全可见,如图 2 所示。

图片1的布局xml:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.paki.venturedive.awtest.MapsActivity" />

图片2的布局xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.6"
    tools:context="com.paki.venturedive.awtest.MapsActivity" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Hello world"
    android:layout_weight="0.4" />

Java 代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

在图2中,我(用户将)无法看到格陵兰地区上方的北冰洋,同样底部区域将丢失,如图3所示。

有人遇到过这个问题吗,或者有人知道如何解决吗?

任何参考链接或提示将不胜感激。

, .

【问题讨论】:

  • 发布您的 xml 。设置高度和宽度的代码。
  • @SagarNayak 问题已编辑。
  • 当您将其权重设置为 0.6 时,线性布局将仅提供 60% 的空间来映射片段。告诉我你为什么要减肥。你到底想要什么?
  • 我正在给予权重,因为应用程序要求在 60% 的区域上显示地图。虽然地图是可滚动的并且它可以滚动,所以它应该显示完整的地图,正如我们在 maps.google.com 上看到的那样
  • 我不明白。您希望地图以 60% 的区域显示在屏幕上。你明白了。然后呢?

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


【解决方案1】:

使用这个 sn-p。 我明白了你的意思,通过在线性/相对布局中获取地图片段,地图会从顶部和底部切割,并且某些部分不可见。由于您希望地图占据屏幕的 60%,因此一种可能的解决方案是,在 FrameLayout 中使用高度为 match_parent 的地图片段。

现在,将 LinearLayout 内的 textview 设置为 FrameLayout,并将其重力设置为底部,并使用 DisplayMetrics 将其高度动态设置为屏幕的 40%(或您的要求是多少)班级。通过这种方式,我们确保为地图提供全屏显示,以免任何部分被剪切,并在地图上方显示文本。我还附上了更新后的屏幕截图。

<?xml version="1.0" encoding="utf-8"?>


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map_framecontainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <fragment
        android:id="@+id/fragment_map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"

        android:layout_height="match_parent" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:id="@+id/tv_main">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/fragment_map"
        android:layout_gravity="center"
        android:background="#ffffff"
        android:text="Hello world" />

    </LinearLayout>
</FrameLayout>

您的 onCreate for DisplayMetrics 中的 java 代码:

   DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);

            mylinearlayout.setMinimumHeight((int)(metrics.heightPixels * 0.4));

我认为这会有所帮助。

【讨论】:

  • 你看到图3了吗,上面的区域没有显示滚动/拖动地图。
  • 兄弟我试过了,它可以拖动地图。我将在我的设备中附加快照。参见上面的 sn-p。或者使用 googleMap.getUiSettings().setScrollGesturesEnabled(true);如果无法滚动,请在 OnMapReady() 函数中。
  • 滚动是工作,我可以滚动,你能不能再抓一个屏幕截图,显示该区域,就像我在图 1 和图 3 中发布的那样。滚动到顶部,让我看看它
  • 哥们,我截图的顶部和底部也被剪掉了。明白你的意思,检查更新的解决方案,你可以这样做。
  • 似乎,添加您的代码后,我将在线性布局的背景上拥有 40% 的底部地图区域.. 对吗?
猜你喜欢
  • 1970-01-01
  • 2015-08-18
  • 1970-01-01
  • 2012-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-23
  • 1970-01-01
相关资源
最近更新 更多