Android MapFragments手动

时间:2013-05-02 00:54:14

标签: android google-maps android-fragments

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

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

0 个答案:

没有答案