我正在使用google map android,我想在片段中使用getSupportFragmentManager()代替活动,但未定义getSupportFragmentManager(),这是什么问题?
这是我的代码
public class MapFragment extends Fragment implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map,
container, false);
// MapFragment mapFragment = (MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.map);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return view;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng TutorialsPoint = new LatLng(21, 57);
mMap.addMarker(new
MarkerOptions().position(TutorialsPoint).title("Tutorialspoint.com"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(TutorialsPoint));
}
答案 0 :(得分:-1)
已编辑: 使用这个:
// Remove old code
// SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
// don't recreate fragment everytime ensure last map location/state are maintained
if (mapFragment == null) {
mapFragment = SupportMapFragment.newInstance();
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
LatLng latLng = new LatLng(1.289545, 103.849972);
googleMap.addMarker(new MarkerOptions().position(latLng)
.title("Singapore"));
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
});
}
// R.id.map is a FrameLayout, not a Fragment
getChildFragmentManager().beginTransaction().replace(R.id.map, mapFragment).commit();
根据此链接Add SupportMapFragment (Android Google Maps) In Fragment