我正在尝试将MapFragment添加到我当前的Fragment中.嵌套片段的使用仅限于FragmentTransactions,您不能在布局中使用xml标记.此外,我希望在用户按下按钮时将其添加到主片段中.所以,getInstance()当用户按下该按钮并将其添加到适当的位置时,我将以编程方式创建MapFragment .它显示正确,到目前为止一切顺利.
问题是在附加MapFragment之后我需要获得一个GoogleMap 放置Marker的引用,但是该getMap()方法返回null(因为片段onCreateView()还没有被调用).
我查看了演示示例代码,我发现他们使用的解决方案是初始化MapFragment onCreate()并在调用onResume()之后获取对GoogleMap的引用onCreateView().
我需要在MapFragment初始化之后立即获取对GoogleMap的引用,因为我希望用户能够使用按钮显示或隐藏地图.我知道一个可能的解决方案是如上所述在开始时创建Map并且只是设置它的可见性消失了,但我希望默认情况下关闭地图,这样如果没有明确要求它就不会占用用户的带宽为了它.
我试过了MapsInitializer,但也没用.我有点卡住了.有任何想法吗?这是我到目前为止的测试代码:
public class ParadaInfoFragment extends BaseDBFragment {
// BaseDBFragment is just a SherlockFragment with custom utility methods.
private static final String MAP_FRAGMENT_TAG = "map";
private GoogleMap mMap;
private SupportMapFragment mMapFragment;
private TextView mToggleMapa;
private boolean isMapVisible = false;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_parada_info, container, false);
mToggleMapa = (TextView) v.findViewById(R.id.parada_info_map_button); …Run Code Online (Sandbox Code Playgroud)