android - How to use google map V2 inside fragment? -


i have fragment part of viewpager, , want use google map v2 inside fragment. have tried far,

in fragment,

    private supportmapfragment map;       private googlemap mmapview;     @override     public void onactivitycreated(bundle savedinstancestate) {         // todo auto-generated method stub         super.onactivitycreated(savedinstancestate);          fragmentmanager fm = getchildfragmentmanager();         map = (supportmapfragment) fm.findfragmentbyid(r.id.map);         if (map == null) {             map = supportmapfragment.newinstance();             fm.begintransaction().replace(r.id.map, map).commit();         }        }      @override     public void onresume() {         super.onresume();         if (mmapview == null) {             mmapview = map.getmap();             marker hamburg = mmapview.addmarker(new markeroptions().position(hamburg)                       .title("hamburg"));                   marker kiel = mmapview.addmarker(new markeroptions()                       .position(kiel)                       .title("kiel")                       .snippet("kiel cool")                       .icon(bitmapdescriptorfactory                           .fromresource(r.drawable.ic_launcher)));                     mmapview.movecamera(cameraupdatefactory.newlatlngzoom(hamburg, 15));                    // zoom in, animating camera.            mmapview.animatecamera(cameraupdatefactory.zoomto(10), 2000, null);           }     } 

and in layout subfragment_info.xml , have,

<fragment             android:id="@+id/map"             class="com.google.android.gms.maps.supportmapfragment"             android:layout_width="match_parent"             android:layout_height="300dp"             android:layout_alignparentleft="true"             android:layout_below="@+id/tablelayout1" /> 

i can see map now. markers not showing. think google map mmapview null. please me problem solved. in advance.

create frame map in added in xml layout

<relativelayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/map_container">  <!-- map fragments go here -->  </relativelayout> 

don't include class="com.google.android.gms.maps.supportmapfragment" in xml either in fragment class manually inside onactivitycreated

    @override public void onactivitycreated(bundle savedinstancestate) {     super.onactivitycreated(savedinstancestate);     fragmentmanager fm = getchildfragmentmanager();     fragment = (supportmapfragment) fm.findfragmentbyid(r.id.map_container);     if (fragment == null) {         fragment = supportmapfragment.newinstance();         fm.begintransaction().replace(r.id.map_container, fragment).commit();     }   /***at time google play services not initialize map , add ever want in onresume() or onstart() **/ }  @override     public void onresume() {         super.onresume();         if (map == null) {             map = fragment.getmap();             map.addmarker(new markeroptions().position(new latlng(0, 0)));         }     } 

if guys face problem illegal state exception occur write below code

/* * issue tracked in (google bugs) * http://code.google.com/p/gmaps-api-issues/issues/detail?id=5064 added * code due illegal state exception occur when reclicking on tab * * short-term workaround fixed me add following * ondetach() of every fragment call */

@override     public void ondetach() {         super.ondetach();          try {             field childfragmentmanager = fragment.class                     .getdeclaredfield("mchildfragmentmanager");             childfragmentmanager.setaccessible(true);             childfragmentmanager.set(this, null);          } catch (nosuchfieldexception e) {             throw new runtimeexception(e);         } catch (illegalaccessexception e) {             throw new runtimeexception(e);         }     } 

//if want show map in activity extend activity //fragmentactivity write code mention below

in oncreate

fragmentmanager fm = getsupportfragmentmanager();         fragment = (supportmapfragment) fm.findfragmentbyid(r.id.map_container);         if (fragment == null) {             fragment = supportmapfragment.newinstance();             fm.begintransaction().replace(r.id.map_container, fragment)                     .commit();     } 

in onresume

@override     protected void onresume() {         super.onresume();         if (googlemap == null) {             initilizemap();          }     }  private void initilizemap() {         if (googlemap != null) {              googlemap = fragment.getmap();             googlemap.setmaptype(googlemap.map_type_normal);             googlemap.getuisettings().setmylocationbuttonenabled(true);             googlemap.getuisettings().setcompassenabled(true);             googlemap.getuisettings().setrotategesturesenabled(true);              cameraposition cameraposition = new cameraposition.builder()                     .target(new latlng(latitude, longitude)).zoom(10).build();             googlemap.animatecamera(cameraupdatefactory                     .newcameraposition(cameraposition));              // create marker             markeroptions marker = new markeroptions().position(new latlng(                     latitude, longitude));             // rose color icon             marker.icon(bitmapdescriptorfactory                     .defaultmarker(bitmapdescriptorfactory.hue_rose));             // adding marker             googlemap.addmarker(marker);              // check if map created or not             if (googlemap == null) {                 toast.maketext(getapplicationcontext(),                         "sorry! unable create maps", toast.length_short)                         .show();             }         }     } 

additional can these links
https://code.google.com/p/gmaps-api-issues/issues/detail?id=5064#c1 https://developers.google.com/maps/documentation/android/map


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -