如何在片段内使用SupportMapFragment?

我知道在使用嵌套片段时存在一个问题。 但我的应用程序被devise为运行碎片,如果我将使用活动的地图,我的铸造function将有错误。

我想问你如何实现这一目标的帮助。 我一直在网上search,但我找不到最好的解决scheme。

我试过这个代码:

private void setUpMapIfNeeded() { // Do a null check to confirm that we have not already instantiated the map. if (mMap == null) { // Try to obtain the map from the SupportMapFragment. mMap = ((SupportMapFragment) myFragmentActivity.getSupportFragmentManager().findFragmentById(R.id.map_con)) .getMap(); // Check if we were successful in obtaining the map. if (mMap != null) { mMap.setMyLocationEnabled(true); } } } 

这会给我重复的错误,因为R.id.map_con是我的片段内的片段。

所以我寻找工作,这一次,R.id.map_con是一个框架布局,在运行时,我创build了SupportMapFragment。

 SupportMapFragment mSupportMapFragment = new SupportMapFragment(); myFragmentActivity.getSupportFragmentManager().beginTransaction() .replace(R.id.map_con, mSupportMapFragment).commit(); 

虽然这并没有给我一个重复的每一次closures和打开片段。 但我的错误是,mSupportMapFragment.getMap始终为空,我不明白为什么它的空?

 mMap = mSupportMapFragment.newInstance().getMap(); if (mMap != null){ Log.e("ReportFragment","mMap is not empty"); }else{ Log.e("ReportFragment","mMap is empty"); } 

我真的很感谢你们的任何投入,或者你有另外的工作,但仍然在这个过程中,即碎片内碎片

谢谢

chkm8

Solutions Collecting From Web of "如何在片段内使用SupportMapFragment?"

我只是遇到了我的运气,在做这个职位的时候,我发现了我在找什么。

我用过这个:

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_location, container, false); mMapFragment = new SupportMapFragment() { @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); mMap = mMapFragment.getMap(); if (mMap != null) { setupMap(); } } }; getChildFragmentManager().beginTransaction().add(R.id.framelayout_location_container, mMapFragment).commit(); return v; } 

感谢这篇旧文章

getMap()已被弃用

代码应该是这样的一个Fragment

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_location, container, false); mMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map); mMapFragment.getMapAsync(this); return v; } 

我已经用它给定的方式,它工作正常。

它也使用getChildFragmentManager()

MapyFragment

 public class MapyFragment extends Fragment implements OnMapReadyCallback { private Context mContext; private SupportMapFragment supportMapFragment; private GoogleMap map; private MarkerOptions currentPositionMarker = null; private Marker currentLocationMarker; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { mContext = getActivity(); return inflater.inflate(R.layout.fragment_mapy, container, false); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); mContext = getActivity(); FragmentManager fm = getActivity().getSupportFragmentManager();/// getChildFragmentManager(); supportMapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map_container); if (supportMapFragment == null) { supportMapFragment = SupportMapFragment.newInstance(); fm.beginTransaction().replace(R.id.map_container, supportMapFragment).commit(); } supportMapFragment.getMapAsync(this); } @Override public void onMapReady(GoogleMap googleMap) { map = googleMap; if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } map.setMyLocationEnabled(true); map.animateCamera(CameraUpdateFactory.zoomTo(15)); /*map.setOnMapLongClickListener(MapyFragment.this); map.setOnMapClickListener(MapFragment.this);*/ } public void updateCurrentLocationMarker(Location currentLatLng){ if(map != null){ LatLng latLng = new LatLng(currentLatLng.getLatitude(),currentLatLng.getLongitude()); if(currentPositionMarker == null){ currentPositionMarker = new MarkerOptions(); currentPositionMarker.position(latLng) .title("My Location"). icon(BitmapDescriptorFactory.fromResource(R.drawable.start_blue)); currentLocationMarker = map.addMarker(currentPositionMarker); } if(currentLocationMarker != null) currentLocationMarker.setPosition(latLng); ///currentPositionMarker.position(latLng); map.moveCamera(CameraUpdateFactory.newLatLng(latLng)); } } } 

fragment_mapy.xml

 <?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:map="http://schemas.android.com/apk/res-auto" android:id="@+id/map_container" class="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent" map:uiZoomControls="true" />