0

I'm new to android programming and have been struggling with this problem for a while. 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.

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?

1 Answers1

1

The getMap() method was deprecated and then removed, so you'll need to use getMapAsync() instead.

There is no need to override onCreateView() when the Fragment extends SupportMapFragment directly.

Instead, just call getMapAsync() from the onResume() override, and use the Google Map reference that is returned in the onMapReady() override:

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.

Community
  • 1
  • 1
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137