无法解析 getMap() 或替换为 getMapAsync()

cannot resolve getMap() or replace with getMapAsync()

我是 android 编程的新手,并且一直在努力解决这个问题。我读到 getMap() 已被弃用并被 getMapAsync() 取代 但是,我似乎找不到使用 getMayAsync() 的方法,因为它使用片段资源,而我直到现在才需要地图的片段资源。

这是我的代码:

public class RunMapFragment extends SupportMapFragment {
    private static final String ARG_RUN_ID = "RUN_ID";
    private GoogleMap mGoogleMap;
    public static RunMapFragment newInstance(long runId) {
        Bundle args = new Bundle();
        args.putLong(ARG_RUN_ID, runId);
        RunMapFragment rf = new RunMapFragment();
        rf.setArguments(args);
        return rf;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent,
                             Bundle savedInstanceState) {
        View v = super.onCreateView(inflater, parent, savedInstanceState);
        mGoogleMap = getMap(); //Error here
        mGoogleMap.setMyLocationEnabled(true);
        return v;
    }
}

如有任何帮助,我们将不胜感激。 是否可以将地图 API 最小 sdk 回滚到可以使用 getMap() 的版本 9?

getMap() 方法 was deprecated and then removed,因此您需要改用 getMapAsync()

Fragment直接扩展SupportMapFragment时不需要重写onCreateView()

相反,只需从 onResume() 覆盖中调用 getMapAsync(),并使用 onMapReady() 覆盖中返回的 Google 地图引用:

public class RunMapFragment extends SupportMapFragment {
    private static final String ARG_RUN_ID = "RUN_ID";
    private GoogleMap mGoogleMap;
    public static RunMapFragment newInstance(long runId) {
        Bundle args = new Bundle();
        args.putLong(ARG_RUN_ID, runId);
        RunMapFragment rf = new RunMapFragment();
        rf.setArguments(args);
        return rf;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mGoogleMap == null) {
            getMapAsync(this);
        }
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mGoogleMap = googleMap;
        mGoogleMap.setMyLocationEnabled(true);
    }
}

请注意,如果您的目标是 api-23 或更高,您需要确保用户在使用 setMyLocationEnabled() 方法之前已在运行时批准了位置权限,以获取更多信息参见 my answer here