片段中的 getMapAsync()

getMapAsync() in Fragment

我在 Fragment 中实现 Google 地图时遇到问题。

这是我片段的一部分class:

public class FragmentStoreFinderMap extends Fragment implements OnMapReadyCallback {

// Google Map
private GoogleMap googleMap;
private int i;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        googleMap = ((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(
              R.id.map)).getMapAsync(this);

我在 getMapAsync(this) 中遇到错误。我告诉 Incompatible type.

它说:

要求:com.google.android.gms.map.GoogleMap

发现:无效

顺便说一句,这是我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment class="com.google.android.gms.maps.SupportMapFragment"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</LinearLayout>

您的代码 return 出现此错误,因为 getMapAsync() 没有 return 任何东西。如果您查看此功能的文档:

public void getMapAsync (OnMapReadyCallback callback)

Sets a callback object which will be triggered when the GoogleMap instance is ready to be used.

Note that:

This method must be called from the main thread. The callback will be executed in the main thread. In the case where Google Play services is not installed on the user's device, the callback will not be triggered until the user installs it. In the rare case where the GoogleMap is destroyed immediately after creation, the callback is not triggered. The GoogleMap object provided by the callback is non-null.

OnMapReadyCallback 是一个接口,需要通过该函数实现和传递。当前没有分配给您的 googleMap 变量,您应该在 implements OnMapReadyCallback

的代码块中设置它的值
@Override
    public void onMapReady(GoogleMap googleMap) {
        this.googleMap = googleMap;


    }

这段代码应该在你的片段中的某个地方。由于您的 Fragment 已经实现了此接口,因此您确实将其作为 this.

传递

在您的 XML 布局中,您应该这样做

<FrameLayout 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">

<com.google.android.gms.maps.MapView
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</FrameLayout>

在你的片段中实现这个

private MapView mapView;
private GoogleMap googleMap;

如果没有 onCreateView 就覆盖 onCreateView 就像下面的代码

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.your_layout, container, false);
}

覆盖 onViewCreated() 里面 onViewCreated() 把这个

mapView = (MapView) view.findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
mapView.onResume();
mapView.getMapAsync(this);//when you already implement OnMapReadyCallback in your fragment

当你已经在片段中实现 OnMapReadyCallback 时,像这样 this/code

@Override
public void onMapReady(GoogleMap map) {
    googleMap = map;
}

希望对您有所帮助。

在我头脑中出现了一些过热之后,多亏了这个 post 和 ,我想说目前有两种方法可以将 MapFragment 添加到您的应用程序:

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
class="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

通过这种方式,您必须使用标准片段并在内部实现它。实际上你是将一个片段插入到另一个片段中:

    @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    
    SupportMapFragment supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
    supportMapFragment.getMapAsync(this);
}

另一种方法是扩展真正的 SupportMapFragment 并以这种方式实现它而不需要 xml:

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

    getMapAsync(this);
}

而且它们无需在 onCreateView 中设置布局即可工作,既不会在 onViewCreated() 中手动调用 mapView.onCreate(savedInstanceState);mapView.onResume();,无论如何也不推荐这样做。

希望对您有所帮助!

 SupportMapFragment mMapFragment = SupportMapFragment.newInstance();
 FragmentTransaction fragmentTransaction =
 getChildFragmentManager().beginTransaction();
 fragmentTransaction.add(R.id.flMap, mMapFragment);
 fragmentTransaction.commit();
 mMapFragment.getMapAsync(this);