Android library project that helps you to track user location and manage the updates.
This is a sample Android application to show how to <B> track user's location and manage the updates</B>
Getting current location through <B>FusedLocationAPI</B> provided by Google is a bit tricky to implement, but the below step-by-step guidelines will explain the procedure.
compile 'com.google.android.gms:play-services-location:10.+’
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Next is permission check, From Android 6.0 user can deny the permission request, so before try to get the location we need to check the status of the location access. For this use PermissionUtil class.
For making a connection with Location API, need to create an instance of <B>GoogleApiCLient</B>.
GoogleApiClient mGoogleApiClient; mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(LocationServices.API).build(); mGoogleApiClient.connect();
public class MyLocationUsingLocationAPI extends AppCompatActivity implements ConnectionCallbacks, OnConnectionFailedListener,OnRequestPermissionsResultCallback, { @Override public void onConnectionFailed(ConnectionResult result) { Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = "+ result.getErrorCode()); } @Override public void onConnected(Bundle arg0) { // Once connected with google api, get the location } @Override public void onConnectionSuspended(int arg0) { mGoogleApiClient.connect(); } }
LocationRequest mLocationRequest = new LocationRequest(); mLocationRequest.setInterval(10000); mLocationRequest.setFastestInterval(5000); mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() .addLocationRequest(mLocationRequest);
mLastLocation = LocationServices.FusedLocationApi .getLastLocation(mGoogleApiClient);
public Address getAddress(double latitude,double longitude) { Geocoder geocoder; List addresses; geocoder = new Geocoder(this, Locale.getDefault()); try { // Here 1 represent max location result to returned, by documents it recommended 1 to 5 addresses = geocoder.getFromLocation(latitude,longitude, 1); return addresses.get(0); } catch (IOException e) { e.printStackTrace(); } return null; }
For more information, check out my detailed guide here : http://droidmentor.com/get-the-current-location-in-android/