如何在片段中使用 Google 地图 Android SDK?

huangapple go评论61阅读模式
英文:

How to use google maps android SDK in a fragment?

问题

我正在使用 Android 地图 SDK 并尝试在地图上绘制一个点。我按照这里给出的步骤进行操作。

我收到一个 NPE(空指针异常),指示我的 Support Map Fragment 为空。我猜测这是因为我在主要的 Fragment 中使用了视图绑定(View Binding),并将视图设置为 binding.getroot()。

声明代码如下:

SupportMapFragment supportMapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map_view);
supportMapFragment.getMapAsync(this::onMapReady);

XML 布局代码如下:

<androidx.fragment.app.FragmentContainerView
    android:id="@+id/map_view"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_gravity="center"
    android:layout_height="match_parent"/>

编辑:
与视图绑定无关。

英文:

I am using android maps sdk and trying to plot a point on it. I followed the steps as given here
I get an NPE saying that my Support Map Fragment is null. I am guessing this happens due to the fact that I am using view binding and setting the view as binding.getroot() in my main fragment.

The code where I declared this.

  SupportMapFragment supportMapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map_view);
  supportMapFragment.getMapAsync(this::onMapReady);

The XML where I have declared this.

&lt;androidx.fragment.app.FragmentContainerView
    android:id=&quot;@+id/map_view&quot;
    android:name=&quot;com.google.android.gms.maps.SupportMapFragment&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_gravity=&quot;center&quot;
    android:layout_height=&quot;match_parent&quot;/&gt;

EDIT:
Had nothing to do with View binding.

答案1

得分: 2

你应该使用 <fragment> 标签而不是 androidx.fragment.app.FragmentContainerViewFragmentContainerView 只是一个容器,用于在运行时添加片段。另一方面,<fragment> 本身就是一个片段,这就是为什么有 android:name 属性。

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          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" />

如果你在使用一个片段,你应该使用 getChildFragmentManager()

SupportMapFragment supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map_view);

更新 -> 是的,抱歉我之前说错了。事实证明 FragmentContainerView 确实支持在 XML 中添加片段。尝试以下代码。

<androidx.fragment.app.FragmentContainerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    class="com.google.android.gms.maps.SupportMapFragment"
    android:id="@+id/fragmentContainerView"
    android:layout_width="match_parent"
    android:tag="mapFragment"
    android:layout_height="match_parent" />

然后你可以使用 #findFragmentByTag 来获取地图片段。

SupportMapFragment supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentByTag("mapFragment");
英文:

You should be using &lt;fragment&gt; tag not androidx.fragment.app.FragmentContainerView. FragmentContainerView is just a Container for fragments on which we can add fragments at runtime . on other hand &lt;fragment&gt; is itself a fragment this why the android:name attribute is there.

&lt;fragment xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:id=&quot;@+id/map&quot;
android:name=&quot;com.google.android.gms.maps.SupportMapFragment&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
 /&gt;

If you using using it a fragment you should be using getChildFragmentManager()

  SupportMapFragment supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map_view);

Update-> Yeah sorry i was wrong earlier . turns out FragmentContainerView does support adding fragment in xml. Try the below code.

&lt;androidx.fragment.app.FragmentContainerView
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
class=&quot;com.google.android.gms.maps.SupportMapFragment&quot;
android:id=&quot;@+id/fragmentContainerView&quot;
android:layout_width=&quot;match_parent&quot;
android:tag=&quot;mapFragment&quot;
android:layout_height=&quot;match_parent&quot; /&gt;

Then you can get the map fragment by using #findFragmentByTag.

SupportMapFragment supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentByTag(&quot;mapFragment&quot;);

huangapple
  • 本文由 发表于 2020年9月17日 13:03:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63931552.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定