how to find a location of user without using map in android -


this question has answer here:

is possible latitude , longitude of user without using mapfragment. want build class method return current location.

hope you...

put manifest file

<uses-permission-sdk-23   android:name="android.permission.access_coarse_location"/> <uses-permission-sdk-23 android:name="android.permission.access_fine_location"/> 

get run time permission api version 23 , above.

then put code on java file

private synchronized void buildgoogleapiclient() {  log.i("tag", "building googleapiclient");     mgoogleapiclient = new googleapiclient.builder(getactivity().getapplicationcontext())             .addconnectioncallbacks(this)             .addonconnectionfailedlistener(this)             .addapi(locationservices.api)             .build();     createlocationrequest();}  private void createlocationrequest() {     log.i("tag", "createlocationrequest");     mlocationrequest = new locationrequest();     long update_interval = 10 * 1000;     mlocationrequest.setinterval(update_interval);     long fastest_interval = 10000;     mlocationrequest.setfastestinterval(fastest_interval);     mlocationrequest.setpriority(locationrequest.priority_high_accuracy);     builder = new locationsettingsrequest.builder()             .addlocationrequest(mlocationrequest);     //**************************     builder.setalwaysshow(true); //this key ingredient     //**************************  }  private void startlocationupdates() {      log.i("tag", "startlocationupdates");      if (build.version.sdk_int >= 23) {         if (contextcompat.checkselfpermission(getactivity().getapplicationcontext(), manifest.permission.access_fine_location) == packagemanager.permission_granted && contextcompat.checkselfpermission(getactivity().getapplicationcontext(), manifest.permission.access_coarse_location) == packagemanager.permission_granted) {             // todo: consider calling             locationservices.fusedlocationapi.requestlocationupdates(mgoogleapiclient,                     mlocationrequest, this);          }     } else {         locationservices.fusedlocationapi.requestlocationupdates(mgoogleapiclient,                 mlocationrequest, this);     }  }  private void stoplocationupdates() {     log.i("tag", "stoplocationupdates");     locationservices.fusedlocationapi.removelocationupdates(mgoogleapiclient, this);  }  @override public void onconnectionsuspended(int i) {     log.i("tag", "onconnectionsuspended");     if (i == cause_service_disconnected) {         toast.maketext(getactivity().getapplicationcontext(), "disconnected. please re-connect.", toast.length_short).show();     } else if (i == cause_network_lost) {         toast.maketext(getactivity().getapplicationcontext(), "network lost. please re-connect.", toast.length_short).show();     }     mgoogleapiclient.connect(); }  @override public void onlocationchanged(location location) {     log.i("tag", "onlocationchanged");     log.i("tag", "current location==>" + location);     currentlatitude = location.getlatitude();     currentlongitude = location.getlongitude(); }       @override public void onconnectionfailed(connectionresult connectionresult) { if (connectionresult.hasresolution()) {         try {             // start activity tries resolve error             connectionresult.startresolutionforresult(getactivity(), connectionresult.resolution_required);         } catch (intentsender.sendintentexception e) {             e.printstacktrace();         }     } else {         log.e("tag", "location services connection failed code==>" + connectionresult.geterrorcode());         log.e("tag", "location services connection failed because of==> " + connectionresult.geterrormessage());     }  }  @override public void ondestroy() {     super.ondestroy();     if (mgoogleapiclient != null)         mgoogleapiclient.disconnect(); } @override public void onconnected(bundle bundle) {     location mcurrentlocation;      log.i("tag", "onconnected");     if (build.version.sdk_int >= 23) {         if (contextcompat.checkselfpermission(getactivity().getapplicationcontext(), manifest.permission.access_fine_location) == packagemanager.permission_granted && contextcompat.checkselfpermission(getactivity().getapplicationcontext(), manifest.permission.access_coarse_location) == packagemanager.permission_granted) {             // todo: consider calling             mcurrentlocation = locationservices.fusedlocationapi.getlastlocation(mgoogleapiclient);              // note can null if last location isn't known.             if (mcurrentlocation != null) {                 // print current location if not null                 log.d("debug", "current location: " + mcurrentlocation.tostring());                 currentlatitude = mcurrentlocation.getlatitude();                 currentlongitude = mcurrentlocation.getlongitude();             } else {                 startlocationupdates();             }           }     } else {         mcurrentlocation = locationservices.fusedlocationapi.getlastlocation(mgoogleapiclient);          // note can null if last location isn't known.         if (mcurrentlocation != null) {             // print current location if not null             log.d("debug", "current location: " + mcurrentlocation.tostring());             currentlatitude = mcurrentlocation.getlatitude();             currentlongitude = mcurrentlocation.getlongitude();         }         // begin polling new location updates.         startlocationupdates();     } } 

for more details refer link: https://developers.google.com/android/guides/setup

https://developer.android.com/training/location/retrieve-current.html


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -