azizbekian
azizbekian

Reputation: 62189

Get GoogleMap from ViewPager's fragment

I'm trying to get GoogleMap object from a Fragment, which is on the second page of ViewPager, whose contents are given with FragmentPagerAdapter.

My ViewPager consist of two pages: an ordinary layout, and a Google map.

I can access to the first layout doing this:

View tempView = LayoutInflater.from(getApplication())
                .inflate(R.layout.start_activity, null);
tempTextView = (TextView) tempView.findViewById(R.id.timer);

Everything is OK here. But I also want to access Google maps.
When the map was in a separate activity, I could get GoogleMap object doing this:

map=((SupportMapFragment)getSupportFragmentManager()
    .findFragmentById(R.id.map)).getMap();

But now the above line returns null

The question is how to get GoogleMap object from fragment now?

They suggest doing this (some people say this works), but it doesn't take effect for me:

map = ((SupportMapFragment) getSupportFragmentManager()
      .findFragmentByTag("android:switcher:" + R.id.pager + ":1")).getMap();

I'm providing the code here.

Upvotes: 4

Views: 2397

Answers (2)

azizbekian
azizbekian

Reputation: 62189

I've found the solution here, but will post here too for other users to see the code and not get stuck for weeks like I did.

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

Upvotes: 7

Terry
Terry

Reputation: 14897

The problem is that your MyFragmentPagerAdapter doesn't use SupportMapFragments, but only adds MyFragments. You should change its getItem function to something like this:

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

For that you have to implement your own type MySupportMapFragment where you load your layout, similar with what you did with MyFragment.

Then you can call your SupportMapFragment directly from the FragmentPagerAdapter. Like:

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

Once added to the adapter, you should get the SupportMapFragment you are expecting.

Upvotes: 1

Related Questions