GoogleMap作为片段而非FragmentActivity

时间:2015-10-05 10:40:36

标签: android android-fragments

我已经尝试了几天而且我不能让它起作用,是否可以从googleMap而不是Fragment延长FragmentActivity

我使用相同的示例代码android studio创建了map fragment示例,并且只是尝试将其设置为Fragment而不是FragmentActivity

有可能吗?

我使用目标sdk21和minsdk 21

1 个答案:

答案 0 :(得分:3)

在您要使用地图的活动/片段中使用以下代码

<fragment
   class="com.google.android.gms.maps.MapFragment"
   android:name="com.google.android.gms.maps.MapFragment"
   android:id="@+id/map"
   android:layout_width="match_parent"
   android:layout_height="match_parent" />

然后在Fragment的onCreateView中,您可以使用以下代码

private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the
        // map.
        if (mMapFragment == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMapFragment = ((MapFragment) getFragmentManager().findFragmentById(R.id.map));
            // Check if we were successful in obtaining the map.
            if (mMapFragment != null) {
                setUpMap();
            }
        }
    }

设置你用下面提到的方法映射东西

private void setUpMap() {

        mMapFragment.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap map) {
             ...
            }
        }
   }

希望它有所帮助...