Android Map Zooming to the current location when app loading do not working in Fragment -


i'm trying write code has map zooming current location when application loading.

here code use zoom map.

  //zoom current location     public location getmylocation() {         locationmanager locationmanager = (locationmanager) getactivity().getsystemservice(context.location_service);         criteria criteria = new criteria();          location location = locationmanager.getlastknownlocation(locationmanager.getbestprovider(criteria, false));         if (location != null)         {             map.animatecamera(cameraupdatefactory.newlatlngzoom(                     new latlng(location.getlatitude(), location.getlongitude()), 13));              cameraposition cameraposition = new cameraposition.builder()                     .target(new latlng(location.getlatitude(), location.getlongitude()))      // sets center of map location user                     .zoom(17)                   // sets zoom                     .bearing(90)                // sets orientation of camera east                     .tilt(40)                   // sets tilt of camera 30 degrees                     .build();                   // creates cameraposition builder             map.animatecamera(cameraupdatefactory.newcameraposition(cameraposition));          }          return location; } 

then called method inside oncreateview of fragment. here code.

 @nullable     @override     public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {          if(!isgoogleplayserviceavailable())         {            return null;         }          if(rootview != null)         {             viewgroup parent = (viewgroup)rootview.getparent();             if(parent != null)             {                 parent.removeview(rootview);             }         }         else         {             rootview = inflater.inflate(r.layout.google_maps, container, false);             map = ((supportmapfragment) getchildfragmentmanager().findfragmentbyid(r.id.map))                     .getmap();               map.addmarker(new markeroptions().position(new latlng(0, 0)).title("marker").snippet("snippet"));              locationmanager locationmanager = (locationmanager) getactivity().getsystemservice(context.location_service);             criteria criteria = new criteria();              string bestprovider = locationmanager.getbestprovider(criteria, false);             if(bestprovider != null)             {                 location location = locationmanager.getlastknownlocation(bestprovider);                 if(location != null)                 {                     onlocationchanged(location);                 }                 locationmanager.requestlocationupdates(bestprovider, 20000, 0, this);             }          }          return rootview;     } 

but, code doesn't seem execute zooming part.

i'm kind of new android , first code i'm using in fragment(i'm telling because may low level question advanced people). so, can please tell me there specific way implement fragments?

thanks in advance. :)

-edit -

here googlemapsfragment.java

public class googlemapsfragment extends android.support.v4.app.fragment implements locationlistener {      view rootview;     static final latlng hamburg = new latlng(53.558, 9.927);      private googlemap map;      public googlemapsfragment()     {     }      @nullable     @override     public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {          if(!isgoogleplayserviceavailable())         {            return null;         }          if(rootview != null)         {             viewgroup parent = (viewgroup)rootview.getparent();             if(parent != null)             {                 parent.removeview(rootview);             }         }         else         {             rootview = inflater.inflate(r.layout.google_maps, container, false);             map = ((supportmapfragment) getchildfragmentmanager().findfragmentbyid(r.id.map))                     .getmap();               map.addmarker(new markeroptions().position(new latlng(0, 0)).title("marker").snippet("snippet"));              locationmanager locationmanager = (locationmanager) getactivity().getsystemservice(context.location_service);             criteria criteria = new criteria();              string bestprovider = locationmanager.getbestprovider(criteria, false);             if(bestprovider != null)             {                 location location = locationmanager.getlastknownlocation(bestprovider);                 if(location != null)                 {                     onlocationchanged(location);                 }                 locationmanager.requestlocationupdates(bestprovider, 20000, 0, this);             }          }          return rootview;     }      //zoom current location     public location getmylocation() {         locationmanager locationmanager = (locationmanager) getactivity().getsystemservice(context.location_service);         criteria criteria = new criteria();          location location = locationmanager.getlastknownlocation(locationmanager.getbestprovider(criteria, false));         if (location != null)         {             map.animatecamera(cameraupdatefactory.newlatlngzoom(                     new latlng(location.getlatitude(), location.getlongitude()), 13));              cameraposition cameraposition = new cameraposition.builder()                     .target(new latlng(location.getlatitude(), location.getlongitude()))      // sets center of map location user                     .zoom(17)                   // sets zoom                     .bearing(90)                // sets orientation of camera east                     .tilt(40)                   // sets tilt of camera 30 degrees                     .build();                   // creates cameraposition builder             map.animatecamera(cameraupdatefactory.newcameraposition(cameraposition));          }          return location;     }       @override     public void onactivitycreated(@nullable bundle savedinstancestate) {         super.onactivitycreated(savedinstancestate);          map.setmylocationenabled(true); // identify current location of device          location currentlocation = getmylocation(); // calling getmylocation method     }      @override     public void onlocationchanged(location location) {         double latitude = location.getlatitude();         double longitude = location.getlongitude();         latlng latlng = new latlng(latitude, longitude);         map.addmarker(new markeroptions().position(latlng));         map.movecamera(cameraupdatefactory.newlatlng(latlng));         map.animatecamera(cameraupdatefactory.zoomto(15));     }      @override     public void onstatuschanged(string s, int i, bundle bundle) {      }      @override     public void onproviderenabled(string s) {      }      @override     public void onproviderdisabled(string s) {      }      private boolean isgoogleplayserviceavailable()     {         int status = googleplayservicesutil.isgoogleplayservicesavailable(getactivity());         if(connectionresult.success == status)         {             return true;         }         else         {             googleplayservicesutil.geterrordialog(status, getactivity(), 0).show();             return false;         }     } } 

1. implement interface

implements onmapreadycallback, googleapiclient.connectioncallbacks,         googleapiclient.onconnectionfailedlistener 

2. add

 private googleapiclient mgoogleapiclient;    private googlemap mmap;  private latlng latlng; 

3. add code in oncreateview method

supportmapfragment mapfragment = (supportmapfragment) getchildfragmentmanager()             .findfragmentbyid(r.id.map);      if (mapfragment != null)         mapfragment.getmapasync(this);     if (mgoogleapiclient == null) {             mgoogleapiclient = new googleapiclient.builder(getactivity())                     .addapi(locationservices.api).addconnectioncallbacks(this)                     .addonconnectionfailedlistener(this).build();             mgoogleapiclient.connect();         } 

4. onmapready method

 @override     public void onmapready(googlemap googlemap) {         mmap = googlemap;           mmap.addmarker(new markeroptions().position(latlng).title("")                 .icon(bitmapdescriptorfactory.fromresource(r.drawable.ic_launcher)));         mmap.movecamera(cameraupdatefactory.newlatlng(sydney));  //        // zoom in, animating camera.         mmap.animatecamera(cameraupdatefactory.zoomto(15), 3000, null);         mmap.getuisettings().setzoomcontrolsenabled(false);         mmap.getuisettings().setcompassenabled(false);         mmap.getuisettings().setmylocationbuttonenabled(true);     } 

5. override method onconnected

 @override     public void onconnected(@nullable bundle bundle) {         if (contextcompat.checkselfpermission(mactivity,                 android.manifest.permission.access_fine_location) == packagemanager.permission_granted && contextcompat.checkselfpermission(mactivity, android.manifest.permission.access_coarse_location) == packagemanager.permission_granted) {             location mlastlocation = locationservices.fusedlocationapi.getlastlocation(                     mgoogleapiclient);             if (mlastlocation != null)               latlng = new latlng(location.getlatitude(),location.getlongitude());             return;         }       //call onmapready override method      onmapready(mmap);     } 

6.

 @override     public void onconnectionsuspended(int i) {      }      @override     public void onconnectionfailed(connectionresult connectionresult) {         if (mgoogleapiclient != null) {             mgoogleapiclient.connect();         }     } 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -