public class MapFragmentOne extends Fragment implements OnMapReadyCallback,
GoogleMap.OnMapClickListener{
private static final int LAYOUT = R.layout.tab_one;
private static View view;
private static GoogleMap mMap;
private SupportMapFragment mapFragment;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
LayoutInflater lf = getActivity().getLayoutInflater();
view = lf.inflate(LAYOUT, container, false);
mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
if (mapFragment == null) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
mapFragment = SupportMapFragment.newInstance();
fragmentTransaction.replace(R.id.map, mapFragment).commit();
}
mapFragment.getMapAsync(this);
// Вот здесь null
mMap = mapFragment.getMap();
Log.d("MAP1", mMap + "");
return view;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// А вот здесь не null
Log.d("MAP2", mMap + "");
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.setBuildingsEnabled(true);
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.setIndoorEnabled(true);
mMap.setOnMapClickListener(this);
}
@Override
public void onMapClick(LatLng point) {
if (mMap != null) {
//Actions on clicks
}
}
public class MapFragmentOne extends Fragment implements OnMapReadyCallback,
GoogleMap.OnMapClickListener {
private static final int LAYOUT = R.layout.tab_one;
private static View view;
private static GoogleMap mMap;
private SupportMapFragment mapFragment;
private OnGetMap listener;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
LayoutInflater lf = getActivity().getLayoutInflater();
view = lf.inflate(LAYOUT, container, false);
mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
if (mapFragment == null) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
mapFragment = SupportMapFragment.newInstance();
fragmentTransaction.replace(R.id.map, mapFragment).commit();
}
mapFragment.getMapAsync(this);
return view;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// А вот здесь не null
Log.d("MAP2", mMap + "");
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.setBuildingsEnabled(true);
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.setIndoorEnabled(true);
mMap.setOnMapClickListener(this);
if (listener != null) {
listener.onGetMap(mMap);
}
}
@Override
public void onMapClick(LatLng point) {
if (mMap != null) {
//Actions on clicks
}
}
public void getMap(OnGetMap listener) {
if (mMap != null) {
listener.onGetMap(mMap);
} else {
this.listener = listener;
}
}
public interface OnGetMap {
void onGetMap(GoogleMap map);
}
}
mapFragmentOne.getMap(new OnGetMap() {
@Override
public void onGetMap(GoogleMap map) {
// do something with map
}
});