android:googlemap安装上的'不可转换的类型'错误

时间:2016-01-16 00:19:42

标签: android google-maps android-fragments

这里有一个很好的教程在Android上设置谷歌地图: https://gist.github.com/joshdholtz/4522551

我可以在我的应用程序中运行它。

但最近谷歌将getMapAsync()替换为getMap()

这是新的谷歌教程: https://developers.google.com/maps/documentation/android-api/start

我尝试将其转换为片段:

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

但是在调试级别我遇到了这个错误:

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

在这一行:

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

我错了什么?

1 个答案:

答案 0 :(得分:0)

MapView不是SupportMapFragment

您有两个选项,要么使用嵌套的SupportMapFragment,要么让Fragment扩展SupportMapFragment。

对于第一个选项,关键是使用getChildFragmentManager()以获取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));
    }
} 

使用第二个选项,Fragment扩展了SupportMapFragment,并且不需要为任何布局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));
  }
}

有关可能有助于您完成后续步骤的详细信息,请查看this answer