2014-04-19 35 views
1

我可以建立谷歌地图的实例,并查看地图,但我不能的GetMap(),它总是空:谷歌地图是空

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); 
      } 
     } 
+0

后乌尔清单 – KOTIOS

+0

更换片段是异步的,所以这个片段可能事件不会已经放在你的布局,所以使的GetMap给你空。 您应该在此mapFragment事务结束后使用getMap方法替换/添加您的mapFragment和请求地图 –

+0

请参阅http://stackoverflow.com/a/14909970/1051804答案 –

回答

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); 
    } 
    } 
} 

};