从ViewPager片段获取Google Map

时间:2013-03-17 15:04:25

标签: java android android-fragments android-viewpager google-maps-android-api-2

我试图从GoogleMap获取Fragment个对象,ViewPager位于FragmentPagerAdapter的第二页,其内容以ViewPager提供。< / p>

我的View tempView = LayoutInflater.from(getApplication()) .inflate(R.layout.start_activity, null); tempTextView = (TextView) tempView.findViewById(R.id.timer); 包含两个页面:普通版面和Google地图。

我可以访问第一个布局:

GoogleMap

这里一切都好。但我也想访问谷歌地图 当地图处于单独的活动中时,我可以让map=((SupportMapFragment)getSupportFragmentManager() .findFragmentById(R.id.map)).getMap(); 对象执行此操作:

null

但现在上面的行返回GoogleMap

问题是如何从片段中获取map = ((SupportMapFragment) getSupportFragmentManager() .findFragmentByTag("android:switcher:" + R.id.pager + ":1")).getMap(); 对象?

他们建议这样做(有些人说这有效),但它并没有对我生效:

{{1}}

我提供了代码here

2 个答案:

答案 0 :(得分:7)

我找到了解决方案here,但也会在此处发布,以便其他用户查看代码,而不会像我一样陷入困境。

<强> MyPagerAdapter.java

public class MyPagerAdapter extends FragmentPagerAdapter
{
    private Context context;
    final LatLng    YEREVAN = new LatLng(40.181, 44.513);

    public MyPagerAdapter(FragmentManager fm, Context context)
    {
        super(fm);
        this.context = context;
    }

    @Override
    public Fragment getItem(int position)
    {
        switch (position)
        {
            case 0:
                return new MyFragment();
            case 1:
                return MyMapFragment.newInstance(YEREVAN);
        }
        return null;
    }

    @Override
    public int getCount()
    {
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position)
    {
        Locale l = Locale.getDefault();
        switch (position)
        {
            case 0:
                return context.getString(R.string.start).toUpperCase(l);
            case 1:
                return context.getString(R.string.map).toUpperCase(l);
        }
        return null;
    }
}

<强> MyMapFragment.java

public class MyMapFragment extends SupportMapFragment
{
    private LatLng  latLon;

    public MyMapFragment()
    {
        super();
    }

    public static MyMapFragment newInstance(LatLng position)
    {
        MyMapFragment frag = new MyMapFragment();
        frag.latLon = position;
        return frag;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View v = super.onCreateView(inflater, container, savedInstanceState);
        initMap();
        return v;
    }

    private void initMap()
    {
        UiSettings settings = getMap().getUiSettings();
        settings.setAllGesturesEnabled(false);
        settings.setMyLocationButtonEnabled(false);

        getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(latLon, 16));
        getMap().addMarker(new MarkerOptions().position(latLon).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));
    }
}

答案 1 :(得分:1)

问题是您的MyFragmentPagerAdapter不使用SupportMapFragment,只会添加MyFragment。 您应该将其getItem函数更改为以下内容:

public Fragment getItem(int position) {
    Fragment fragment;
    switch (position) {
    case 0:
        fragment = new MyFragment();
        break;
    case 1:
        fragment = new MySupportMapFragment();
        break;
    }
    Bundle args = new Bundle();
    args.putInt(MyFragment.ARG_SECTION_NUMBER, position);
    fragment.setArguments(args);
    return fragment;
}

为此,您必须在加载布局时实现自己的类型MySupportMapFragment,类似于您对MyFragment的处理方式。

然后,您可以直接从FragmentPagerAdapter调用SupportMapFragment。像:

SupportMapFragment supportMapFragment = 
    (SupportMapFragment) mMyFragmentPagerAdapter.getItem(1);
map = supportMapFragment.getMap();

一旦添加到适配器,您应该得到您期望的SupportMapFragment。

相关问题