【问题标题】:android: 'inconvertible types' error on googlemap setupandroid:googlemap设置上的“不可转换类型”错误
【发布时间】:2016-01-16 00:19:42
【问题描述】:

这里有一个很好的在 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);

我怎么了?

【问题讨论】:

    标签: android google-maps android-fragments


    【解决方案1】:

    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

    【讨论】:

      猜你喜欢
      • 2018-06-11
      • 2012-10-02
      • 1970-01-01
      • 2011-06-17
      • 2016-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多