简体   繁体   English

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

[英]cannot resolve getMap() or replace with getMapAsync()

I'm new to android programming and have been struggling with this problem for a while. 我是android编程的新手,已经为这个问题苦苦挣扎了一段时间。 I read that getMap() has been deprecated and replaced by getMapAsync() However, I cannot seem to find a way of using getMayAsync() because it uses the a fragment resource and i did not require a fragment resource for the map till now. 我读到getMap()已被getMapAsync()弃用,但是,我似乎找不到使用getMayAsync()的方法,因为它使用了片段资源,并且直到现在我都不需要片段资源。

Here is my code: 这是我的代码:

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;
    }
}

Any help would be much appreciated. 任何帮助将非常感激。 Is it possible to rollback the map API minimum sdk to verison 9 where getMap() can be used? 是否可以将地图API的最低SDK回滚到版本9(可以使用getMap())?

The getMap() method was deprecated and then removed , so you'll need to use getMapAsync() instead. 不推荐使用 getMap()方法,然后将其删除 ,因此您需要改用getMapAsync()

There is no need to override onCreateView() when the Fragment extends SupportMapFragment directly. 当Fragment直接扩展SupportMapFragment时,无需重写onCreateView()

Instead, just call getMapAsync() from the onResume() override, and use the Google Map reference that is returned in the onMapReady() override: 相反,只需从onResume()覆盖调用getMapAsync() ,然后使用在onMapReady()覆盖中返回的Google Map引用:

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);
    }
}

Note that if you are targeting api-23 or higher, you'll need to ensure that the user has approved the location permission at runtime before using the setMyLocationEnabled() method, for more info see my answer here . 请注意,如果您的目标是api-23或更高版本,则需要在使用setMyLocationEnabled()方法之前确保用户已在运行时批准了位置权限,有关更多信息,请参见此处的我的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM