2014-04-19 57 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); 
    } 
    } 
} 

};