Google地图为空

时间:2014-04-19 20:15:29

标签: android google-maps

我可以构建一个Google Map实例并查看地图,但我不能得到Map(),它总是为空:

mMapFragment = SupportMapFragment.newInstance();
        FragmentTransaction fragmentTransaction = getSupportFragmentManager()
                .beginTransaction();
        fragmentTransaction.replace(R.id.container, mMapFragment);
        fragmentTransaction.commit();
        if (mMap == null) {
            mMap = mMapFragment.getMap();
                    // always null, why?
            if (mMap != null) {
                mMap.getUiSettings().setMyLocationButtonEnabled(true);
            }
        }

2 个答案:

答案 0 :(得分:0)

This works pretty well for me ...
It seems like it's the default background (which is black) of SurfaceView which is causing this bug .
public class MyMapFragment extends SupportMapFragment() {
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  View view = super.onCreateView(inflater, container, savedInstanceState);
  setMapTransparent((ViewGroup) view);
  return view;
 };

 private void setMapTransparent(ViewGroup group) {
  int childCount = group.getChildCount();
  for (int i = 0; i < childCount; i++) {
   View child = group.getChildAt(i);
   if (child instanceof ViewGroup) {
    setMapTransparent((ViewGroup) child);
   } else if (child instanceof SurfaceView) {
    child.setBackgroundColor(0x00000000);
   }
  }
 }

};

答案 1 :(得分:0)

以编程方式创建时,地图将在onActivityCreated()中可用。所以,试试这样:

mMapFragment = new SupportMapFragment() {
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

           if (mMap == null) {
                mMap = this.getMap();

                //other code
        }
    };

点击此处:How do I know the map is ready to get used when using the SupportMapFragment?