0

there is a good tutorial to setup google map on android here: https://gist.github.com/joshdholtz/4522551

I can run it in my application.

But recently google replace getMapAsync() with getMap()

here is new google tutorial: https://developers.google.com/maps/documentation/android-api/start

I try convert it to fragment:

public class MFragment extends Fragment implements OnMapReadyCallback {

    MapView gMapView;
    GoogleMap gMap = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.m_layout, container, false);

        gMapView = (MapView) view.findViewById(R.id.map);
        gMapView.getMapAsync(this);

        return view;
    }

    @Override
    public void onMapReady(GoogleMap map) {
        gMap = map;
        gMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new
                LatLng(49.39,-124.83), 20));
    }
}

m_layout.xml:

<fragment 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"
    android:id="@+id/map"
    tools:context="MFragment"
    android:name="com.google.android.gms.maps.SupportMapFragment" />

But in debug level I got this error:

Error:(24, 75) error: inconvertible types
required: MapFragment
found:    Fragment

on this line:

gMapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);

what's my wrong?

salome
  • 53
  • 4
  • 8

1 Answers1

0

A MapView is not a SupportMapFragment.

You have two options, either use a nested SupportMapFragment, or make your Fragment extend SupportMapFragment.

For the first option, the key is to use getChildFragmentManager() in order to get the SupportMapFragment:

public class MFragment extends Fragment implements
            OnMapReadyCallback {

    GoogleMap gMap;
    SupportMapFragment mapFrag;

    public MFragment () {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.m_layout, container, false);

        mapFrag = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map));
        mapFrag.getMapAsync(this);

        return rootView;
    }

    @Override
    public void onMapReady(GoogleMap map) {
      gMap = map;
      gMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
      gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new
                                                        LatLng(49.39,-124.83), 20));
    }
} 

With the second option, the Fragment extends SupportMapFragment, and there is no need to inflate any layout xml:

public class MFragment  extends SupportMapFragment implements
            OnMapReadyCallback {

  GoogleMap gMap;

  public MFragment() {
  }

  @Override
  public void onResume() {
    super.onResume();
    setUpMapIfNeeded();
  }

  private void setUpMapIfNeeded() {

    if (gMap == null) {
      getMapAsync(this);
    }
  }

  @Override
  public void onMapReady(GoogleMap map) {
    gMap = map;
    gMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new
                                                      LatLng(49.39,-124.83), 20));
  }
}

For more details that might help you in your next steps, take a look at this answer.

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