How automatically to zoom on current location and always to be focused on it in google maps api v3(android)? -


how realize automaticly update of current location in android? necessary location in focus , focus updated in case of location change.

i used official google example(with button):

public class mapsactivity extends fragmentactivity implements onmapreadycallback,         googlemap.onmylocationbuttonclicklistener, activitycompat.onrequestpermissionsresultcallback{      /**      * flag indicating whether requested permission has been denied after returning in      * {@link #onrequestpermissionsresult(int, string[], int[])}.      */     private boolean mpermissiondenied = false;      /**      * request code location permission request.      *      * @see #onrequestpermissionsresult(int, string[], int[])      */     private static final int location_permission_request_code = 1; private static final int location_permission_request_code = 1;      private googlemap mmap;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_maps);         // obtain supportmapfragment , notified when map ready used.         supportmapfragment mapfragment = (supportmapfragment) getsupportfragmentmanager()                 .findfragmentbyid(r.id.map);         mapfragment.getmapasync(this);      }       /**      * manipulates map once available.      * callback triggered when map ready used.      * can add markers or lines, add listeners or move camera. in case,      * add marker near sydney, australia.      * if google play services not installed on device, user prompted install      * inside supportmapfragment. method triggered once user has      * installed google play services , returned app.      */     @override     public void onmapready(googlemap googlemap) {         mmap = googlemap;          mmap.setonmylocationbuttonclicklistener(this);         enablemylocation();          // add marker in sydney , move camera         latlng sydney = new latlng(-34, 151);         mmap.addmarker(new markeroptions().position(sydney).title("marker in sydney"));         mmap.movecamera(cameraupdatefactory.newlatlng(sydney));     }      /**      * enables location layer if fine location permission has been granted.      */     private void enablemylocation() {         if (contextcompat.checkselfpermission(this, manifest.permission.access_fine_location)                 != packagemanager.permission_granted) {             // permission access location missing.             permissionutils.requestpermission(this, location_permission_request_code,                     manifest.permission.access_fine_location, true);         } else if (mmap != null) {             // access location has been granted app.             mmap.setmylocationenabled(true);         }     }      @override     public boolean onmylocationbuttonclick() {         toast.maketext(this, "mylocation button clicked", toast.length_short).show();         // return false don't consume event , default behavior still occurs         // (the camera animates user's current position).         return false;     }       @override     public void onrequestpermissionsresult(int requestcode, @nonnull string[] permissions,                                            @nonnull int[] grantresults) {         if (requestcode != location_permission_request_code) {             return;         }          if (permissionutils.ispermissiongranted(permissions, grantresults,                 manifest.permission.access_fine_location)) {             // enable location layer if permission has been granted.             enablemylocation();         } else {             // display missing permission error dialog when fragments resume.             mpermissiondenied = true;         }     }      @override     protected void onresumefragments() {         super.onresumefragments();         if (mpermissiondenied) {             // permission not granted, display error dialog.             showmissingpermissionerror();             mpermissiondenied = false;         }     }      /**      * displays dialog error message explaining location permission missing.      */     private void showmissingpermissionerror() {         permissionutils.permissiondenieddialog                 .newinstance(true).show(getsupportfragmentmanager(), "dialog");     } 

p.s. maybe makes in api v2? have tutorial this?

private synchronized void buildgoogleapiclient() {         mgoogleapiclient = new googleapiclient.builder(this.getcontext())                 .addconnectioncallbacks(this)                 .addonconnectionfailedlistener(this)                 .addapi(locationservices.api)                 .build();     } 

call buildgoogleapiclient() in onmapready() callback

 @override     public void onconnected(@nullable bundle bundle) {         if (activitycompat.checkselfpermission(this.getcontext(), manifest.permission.access_fine_location) != packagemanager.permission_granted && activitycompat.checkselfpermission(this.getcontext(), manifest.permission.access_coarse_location) != packagemanager.permission_granted) {             activitycompat.requestpermissions(getactivity(),                     new string[]{                             manifest.permission.read_external_storage,                             manifest.permission.write_external_storage,                             manifest.permission.access_fine_location                      },                     100);             return;         }         location mlastlocation = locationservices.fusedlocationapi.getlastlocation(                 mgoogleapiclient);         if (mlastlocation != null) {             //place marker @ current position             //mgooglemap.clear();             latlng = new latlng(mlastlocation.getlatitude(), mlastlocation.getlongitude());             markeroptions markeroptions = new markeroptions();             markeroptions.position(latlng);             markeroptions.title("current position");             markeroptions.icon(bitmapdescriptorfactory.defaultmarker(bitmapdescriptorfactory.hue_magenta));             currlocationmarker = mmap.addmarker(markeroptions);             cameraupdate yourlocation = cameraupdatefactory.newlatlngzoom(latlng, 5);             mmap.animatecamera(yourlocation);         }          mlocationrequest = new locationrequest();         mlocationrequest.setinterval(5000); //5 seconds         mlocationrequest.setfastestinterval(3000); //3 seconds         mlocationrequest.setpriority(locationrequest.priority_balanced_power_accuracy);         //mlocationrequest.setsmallestdisplacement(0.1f); //1/10 meter   } 

for work should key enable locationsapi google api console


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -