Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 727
azizbekian Avatar asked Feb 17 '23 03:02

azizbekian


1 Answers

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)));
    }
}
like image 163
azizbekian Avatar answered Feb 26 '23 20:02

azizbekian