android - Getting Google Map Fragment in onCreateView using ActionBar tabs -
i'm trying retrieve google map fragment while using tabs in actionbar. when load tabbed page map loads fine, want map object can center it, add markers etc.
is there way , if there is, kind show me how?
here's code tabbed page...
the specific method i'm working public static class map extends fragment
public class page3activity extends fragmentactivity implements actionbar.tablistener { final context context = this; static googlemap map; static mapview mmapview; databasehandler db = new databasehandler(context); appsectionspageradapter mappsectionspageradapter; viewpager mviewpager; @suppresslint("newapi") public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.page3); // create adapter return fragment each of 3 primary sections // of app. mappsectionspageradapter = new appsectionspageradapter(getsupportfragmentmanager()); colordrawable colordrawable = new colordrawable(); final actionbar actionbar = getactionbar(); colordrawable.setcolor(0xff9acc00); actionbar.setbackgrounddrawable(colordrawable); actionbar.settitle("road trip calculator"); actionbar.setsubtitle("trip information"); actionbar.setdisplayshowtitleenabled(true); actionbar.setdisplayshowhomeenabled(true); actionbar.setdisplayoptions(actionbar.display_show_custom | actionbar.display_show_home | actionbar.display_show_title ); // specify home/up button should not enabled, since there no hierarchical // parent. actionbar.sethomebuttonenabled(false); // specify displaying tabs in action bar. actionbar.setnavigationmode(actionbar.navigation_mode_tabs); // set viewpager, attaching adapter , setting listener when // user swipes between sections. mviewpager = (viewpager) findviewbyid(r.id.pager); mviewpager.setadapter(mappsectionspageradapter); mviewpager.setonpagechangelistener(new viewpager.simpleonpagechangelistener() { @override public void onpageselected(int position) { // when swiping between different app sections, select corresponding tab. // can use actionbar.tab#select() if have reference // tab. actionbar.setselectednavigationitem(position); } }); // each of sections in app, add tab action bar. (int = 0; < mappsectionspageradapter.getcount(); i++) { // create tab text corresponding page title defined adapter. // specify activity object, implements tablistener interface, // listener when tab selected. actionbar.addtab( actionbar.newtab() .settext(mappsectionspageradapter.getpagetitle(i)) .settablistener(this)); } } /* * actionbar top bar code */ @override public void ontabunselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { } @override public void ontabselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { // when given tab selected, switch corresponding page in viewpager. mviewpager.setcurrentitem(tab.getposition()); } @override public void ontabreselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { } /** * {@link fragmentpageradapter} returns fragment corresponding 1 of primary * sections of app. */ public static class appsectionspageradapter extends fragmentpageradapter { public appsectionspageradapter(fragmentmanager fm) { super(fm); } //switching between tabs @override public fragment getitem(int i) { switch (i) { case 0: return new map(); case 1: return new directions(); case 2: return new poilist(); case 3: return new tripinfo(); default: i=i+1; } return null; } @override public int getcount() { return 4; } @override public string getpagetitle(int position) { string tabname = ""; if(position == 0) { tabname = "map"; } else if(position == 1) { tabname = "directions"; } else if(position == 2) { tabname = "poi list"; } else if(position == 3) { tabname = "trip info"; } return tabname; } } /** * map */ public static class map extends fragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.maptab, container, false); //i want able this... /* googlemap map; supportmapfragment fm = (supportmapfragment) getsupportfragmentmanager().findfragmentbyid(r.id.map1); map = fm.getmap(); map.dosomething */ return rootview; } } /** * directions */ public static class directions extends fragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.directionstab, container, false); ((textview) rootview.findviewbyid(android.r.id.text1)).settext( "directions"); return rootview; } } /** * poi list */ public static class poilist extends fragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.poitab, container, false); ((textview) rootview.findviewbyid(android.r.id.text1)).settext( "poilist"); return rootview; } } /** * trip info */ public static class tripinfo extends fragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.tripinfotab, container, false); ((textview) rootview.findviewbyid(android.r.id.text1)).settext( "trip info"); return rootview; } } }
here xml file (maptab.xml) have map fragment in...
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <fragment android:id="@+id/map1" android:layout_width="match_parent" android:layout_height="match_parent" class="com.google.android.gms.maps.supportmapfragment" /> </linearlayout>
solved
public static class map extends fragment { mapview mapview; googlemap map; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.maptab, container, false); // gets mapview xml layout , creates mapview = (mapview) v.findviewbyid(r.id.mapview); mapview.oncreate(savedinstancestate); // gets googlemap mapview , initialization stuff map = mapview.getmap(); //map.getuisettings().setmylocationbuttonenabled(false); //map.setmylocationenabled(true); map.addmarker(new markeroptions().position(new latlng(50.167003,19.383262))); // needs call mapsinitializer before doing cameraupdatefactory calls try { mapsinitializer.initialize(this.getactivity()); } catch (googleplayservicesnotavailableexception e) { e.printstacktrace(); } // updates location , zoom of mapview cameraupdate cameraupdate = cameraupdatefactory.newlatlngzoom(new latlng(43.1, -87.9), 10); map.animatecamera(cameraupdate); return v; } @override public void onresume() { mapview.onresume(); super.onresume(); } @override public void ondestroy() { super.ondestroy(); mapview.ondestroy(); } @override public void onlowmemory() { super.onlowmemory(); mapview.onlowmemory(); } }
you can reference fragment containing map using mviewpager.getitem(0)
, cast map
fragment , use way like. not design operate on fragment outside. recommend implement business logic related map inside map
fragment, way don't need solve kind of issue, posted , business logic encapsulated inside fragment, portable.
Comments
Post a Comment