1

我试图通过手动初始化它而不是在 xml 文件中设置它来让谷歌地图 v2 工作。我正在尝试遵循示例应用程序,但是当我运行我的应用程序时,我得到的只是没有任何位置的地图。我认为这可能与片段标签有关,因为我不确定那是在做什么。这是从演示应用程序设置片段的代码:

private static final String MAP_FRAGMENT_TAG = "map";
private GoogleMap mMap;
private SupportMapFragment mMapFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // It isn't possible to set a fragment's id programmatically so we set a tag instead and
        // search for it using that.
        mMapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentByTag(MAP_FRAGMENT_TAG);

        // We only create a fragment if it doesn't already exist.
        if (mMapFragment == null) {
            // To programmatically add the map, we first create a SupportMapFragment.
            mMapFragment = SupportMapFragment.newInstance();

            // Then we add it using a FragmentTransaction.
            FragmentTransaction fragmentTransaction = getSupportFragmentManager()
                    .beginTransaction();
            fragmentTransaction.add(android.R.id.content, mMapFragment, MAP_FRAGMENT_TAG);
            fragmentTransaction.commit();
        }

        // We can't be guaranteed that the map is available because Google Play services might
        // not be available.
        setUpMapIfNeeded();
    }

setUpMapIfNeeded
{
    if (mMap == null) {
                // Try to obtain the map from the SupportMapFragment.
                mMap = mMapFragment.getMap();
    }
}
4

0 回答 0