SupportMapFragment和onMapReady

4

大家好,

我已经搜索了几个小时并尝试了不同的方法,但大多数方法我都已经包括在内。希望你们能够帮助我看到我可能没有注意到的问题。

我在我的地图片段中有一个SupportMapFragment。我曾经使它工作过,但由于某种原因,它现在会出现nullpointer错误。在搜索了几个小时并阅读了一些文档后,我发现我使用了一个被弃用的方式,因此我尝试使用建议的方式:onMapReady。然而,即使我使用了这种方法,它也无法识别该函数。

com.google.android.gms.maps.SupportMapFragment

这是我的XML片段:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frameLayoutMainAct"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

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

    <View
        android:id="@+id/map_overlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

以下是从OnViewCreated开始的代码:

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    if(GooglePlayServicesUtil.isGooglePlayServicesAvailable(MainActivity.getContext()) == ConnectionResult.SUCCESS) {
        initMap();
        if (locations != null) {
            addMarkers();
        }
        moveMapToCuracao();
    }
}

public void initMap() {
    final SupportMapFragment myMAPF = (SupportMapFragment) getFragmentManager()
            .findFragmentById(mapView);
    myMAPF.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap googleMap) {
            mGoogleMap = googleMap;

            mGoogleMap.setPadding(50, 0, 0, 0);
            mGoogleMap.getUiSettings().setMyLocationButtonEnabled(false);
            mGoogleMap.setMyLocationEnabled(true);
            mGoogleMap
                    .setOnMyLocationChangeListener(new OnMyLocationChangeListener() {

                        @Override
                        public void onMyLocationChange(Location location) {
                            // TODO Auto-generated method stub
                            GeomagneticField field = new GeomagneticField(
                                    (float) location.getLatitude(),
                                    (float) location.getLongitude(),
                                    (float) location.getAltitude(), System
                                    .currentTimeMillis());

                            // getDeclination returns degrees
                            mDeclination = field.getDeclination();
                        }

                    });

            if (locations != null) {
                addMarkers();
            }
            moveMapToCuracao();
        }
    });
}

更新:感谢Eugen提供的代码更新。

但仍然在调用getMapAsync()时崩溃,出现NullPointerException。


1
  1. 你确定要在另一个片段中使用地图片段吗?
  2. 使用getMapAsync,在回调函数中进行地图初始化,并且不要保留mGoogleMap,因为在getMapAsync之外使用它是不安全的。
- Eugen Pechanec
getMapAsync应该在哪里使用?否则我该怎么做? - Jordi Sipkens
1
请使用getMapAsync代替getMap,参见此链接。您可以扩展SupportMapFragment - Eugen Pechanec
那就是问题所在。我无法像上面所述的那样识别它。但我会尝试使用SupportMapFragment。 - Jordi Sipkens
1
你是否使用 Google Play Services 库至少 v6.5.87 ?getMapAsync 方法正是在那个版本中引入的。 - Eugen Pechanec
显示剩余3条评论
1个回答

6

地图片段是您填充到另一个片段(我们称其为父片段)的布局的一部分。填充的片段成为活动的子片段,而不是父片段。您必须询问父片段的子片段管理器:

final SupportMapFragment myMAPF = (SupportMapFragment) getChildFragmentManager()
        .findFragmentById(mapView);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接