2

我正在尝试在我的 Android 应用程序中使用 Google Maps API v2。我已经使用以下代码以编程方式添加了地图片段,然后我尝试从我的 SupportMapFragment 获取 GoogleMap,但即使地图显示在屏幕上,我总是得到空结果...任何帮助都非常感谢!!!!!! !!

谢谢

public class MapActivity extends BaseFragmentActivity {


    private SchoolType mSchoolType=SchoolType.ALL;
    private GoogleMap mMap;
    private UiSettings mUiSettings;
    private SupportMapFragment mMapFragment;
    private static final String MAP_FRAGMENT_TAG = "map";
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try{
            setContentView(R.layout.map_activity);
            mMapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentByTag(MAP_FRAGMENT_TAG);
            if(mMapFragment==null)
                addMapFragment();
            setUpMapIfNeeded();

        }
        catch(Exception ex){
            System.err.println("Exception: " + ex.getMessage());

        }

    }

    private void addMapFragment(){
        try{

            GoogleMapOptions options = new GoogleMapOptions();
            options.mapType(GoogleMap.MAP_TYPE_NORMAL)
                    .zoomControlsEnabled(true) ;

            mMapFragment =  SupportMapFragment.newInstance(options);
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.add(R.id.fragment_map_content, mMapFragment,MAP_FRAGMENT_TAG);
            //transaction.addToBackStack(null);
            transaction.commit();
        }
        catch(Exception ex){
            System.err.println("Exception: " + ex.getMessage());
        }

    }

private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap =  mMapFragment.getMap(); ***//ALWAYS RETUN NULL***
            //mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))

            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }
4

1 回答 1

0

call setUpMapIfNeeded in onResume(), like this.

@Override
protected void onResume() {
    super.onResume();

    // In case Google Play services has since become available.
    setUpMapIfNeeded();
}
于 2013-01-19T18:55:33.573 回答