android - How to Add Google Map into fragment onActivityCreated -
i put google map in fragment, please noted want put inside onactivitycreated, , fragment class need extend fragment class only.
and final layout be: fragment contains text, map , button.
~~~~~~~~~~~~~~~
textview.....
map.....
button....
~~~~~~~~~~~~~~~
also, map need include current location.
may know how that? java , xml please :)
in fragment layout put mapview widget:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:map="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- textview --> <com.google.android.gms.maps.mapview android:id="@+id/my_map" android:layout_width="match_parent" android:layout_height="match_parent" map:uizoomcontrols="false" map:uicompass="false" map:uirotategestures="false" map:uiscrollgestures="true" map:uitiltgestures="false" map:uizoomgestures="true" android:visibility="visible"/> <!-- button , others widgets --> </relativelayout>
your fragment inflate custom layout mapview , others widgets need in . here pseudo code how setup mapview.
public class mycustomfragmentwithmap extends fragment { private mapview mmapview = null; private googlemap mmymap = null; @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); try { mapsinitializer.initialize(getactivity()); } catch (googleplayservicesnotavailableexception err) { log.e(tag, err.getmessage(), err); } mmapview = (mapview)getview().findviewbyid(r.id.my_map); mmapview.oncreate(savedinstancestate); mmymap = mmapview.getmap(); mmymap.setmylocationenabled(true); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.my_custom_fragment_with_map, container, false); /* widgets setup usual ... */ return view; } @override public void onresume() { super.onresume(); if (mmapview != null) mmapview.onresume(); } @override public void onpause() { if (mmapview != null) mmapview.onpause(); super.onpause(); } @override public void ondestroy() { if (mmapview != null) mmapview.ondestroy(); super.ondestroy(); } @override public void onlowmemory() { super.onlowmemory(); if (mmapview != null) mmapview.onlowmemory(); } @override public void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); if (mmapview != null) mmapview.onsaveinstancestate(outstate); }
i'm assuming know how setup google maps android api v2 (obtaining api key, setting permissions in manifest , boring stuffs :-p).
Comments
Post a Comment