Search code examples
androidandroid-viewpageropenstreetmaposmdroid

Adding an openstreetmap in a viewpager


I have some questions about the viewpager in android studio,

So i made a viewpager with 3 screens using a viewpager and a PagerAdapter class public class MainActivity extends FragmentActivity {

private MapView myOpenMapView;
private MapController myMapController;
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    MyPagerAdapter adapter = new MyPagerAdapter();
    ViewPager myPager = (ViewPager) findViewById(R.id.viewpager_layout);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(1);

    expListView = (ExpandableListView) findViewById(R.id.expandableListView2);
    prepareListData();
    listAdapter = new ExpandableListAdapter(this,listDataHeader,listDataChild);
}
    public class MyPagerAdapter extends PagerAdapter {
            public int getCount() {
                return 3;
            }

            public Object instantiateItem(ViewGroup collection, int position) {
                View view=null;

                LayoutInflater inflater = (LayoutInflater) collection.getContext()
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                int resId = 0;
                switch (position) {
                    case 0:
                        resId = R.layout.activity_friend;
                        view = inflater.inflate(resId, null);
                        break;
                    case 1:
                        resId = R.layout.fragment_main_page;
                        view = inflater.inflate(resId, null);
                        break;
                    case 2:
                        resId = R.layout.activity_map;
                        view = inflater.inflate(resId, null);
                        myOpenMapView = (MapView) findViewById(R.id.openmapview);
                        //myOpenMapView.setBuiltInZoomControls(true);
                        //myMapController = myOpenMapView.getController();
                        //myMapController.setZoom(4);
                        //myOpenMapView.setTileSource(TileSourceFactory.MAPQUESTOSM);

                        break;
                }
                ((ViewPager) collection).addView(view, 0);
                return view;
            }

            @Override
            public void destroyItem(ViewGroup arg0, int arg1, Object arg2) {
                ((ViewPager) arg0).removeView((View) arg2);
            }

            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                return arg0 == ((View) arg1);
            }

            //public boolean isViewFromObject(ViewGroup arg0, Object arg1) {
            // return arg0 == ((View) arg1);
            //}
            @Override
            public Parcelable saveState() {
                return null;
            }
        }

This works just fine, now in my project i want to implement an openstreetmap, i added this code

            myOpenMapView = (MapView) findViewById(R.id.openmapview);
            myOpenMapView.setBuiltInZoomControls(true);
            myMapController = myOpenMapView.getController();
            myMapController.setZoom(4);
            myOpenMapView.setTileSource(TileSourceFactory.MAPQUESTOSM); 

this code works fine if it's put in a seperate activity but for some reason if it's inside the oncreate in the viewpager or inside the pageradapter class it doesn't work

Can anyone explain why it doesn't work and how i can fix it?


Solution

  •                     resId = R.layout.activity_map;
                        v = inflater.inflate(resId,null);
                        View map = v.findViewById(R.id.openmapview);
                        myOpenMapView = (MapView)map;
                        myOpenMapView.setBuiltInZoomControls(true);
                        myOpenMapView.setMultiTouchControls(true);
                        myOpenMapView.setClickable(true);
                        myMapController = myOpenMapView.getController();
                        myMapController.setZoom(15);
                        myOpenMapView.getController().setCenter(
                                new GeoPoint(MAP_LATITUDE, MAP_LONGITUDE));
                        myOpenMapView.setTileSource(TileSourceFactory.MAPQUESTOSM);
                        break;
    

    So i figured it out,

    you create a new view

    View map = v.findViewById(R.id.openmapview); 
    

    as such but you create it out of the inflated view

    and then you fill in the map with the id you just acquired

    myOpenMapView = (MapView)map;
    

    Hope this can help some people who ran into the same problem