Skip to content Skip to sidebar Skip to footer

How To Get Real Time Current Location Update As We Walk

I want to discuss if there is possibility of getting real time current location update as we move, I tried with FusedLocationProviderClient to request for current location update e

Solution 1:

You can use this class that i created :

publicclassLocationFetcherimplements com.google.android.gms.location.LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
publicGoogleApiClient googleApiClient;
Context context;
privateLocationRequest locationRequest;

publicLocationFetcher(Context context) {
    this.context = context;
    googleApiClient = newGoogleApiClient.Builder(context)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    locationRequest = newLocationRequest();


}

publicvoidconnectGps() {
    googleApiClient.connect();
}

publicvoidrequestLocationUpdate() {
    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions((Activity) context, newString[]{Manifest.permission.ACCESS_FINE_LOCATION}, 100);
    } else {
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
     //Getting location every 3 seconds. You can change this.
            locationRequest.setInterval(3000);
            locationRequest.setFastestInterval(3000);


        LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
    }
}

publicbooleanisConnected() {
    return googleApiClient.isConnected();
}

@OverridepublicvoidonLocationChanged(Location location) {
//You will get location updates here.
}


@OverridepublicvoidonConnected(@Nullable Bundle bundle) {

    LocationSettingsRequest.Builder builder = newLocationSettingsRequest.Builder()
            .addLocationRequest(locationRequest);

    builder.setAlwaysShow(true);
    PendingResult result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());

    if (result != null) {
        result.setResultCallback(newResultCallback<LocationSettingsResult>() {
            @OverridepublicvoidonResult(LocationSettingsResult locationSettingsResult) {
                final Status status = locationSettingsResult.getStatus();
                switch (status.getStatusCode()) {
                    caseLocationSettingsStatusCodes.SUCCESS:
                        requestLocationUpdate();
                        break;
                    caseLocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        try {
                            if (status.hasResolution()) {
                                status.startResolutionForResult((Activity) context, 1000);
                            }
                        } catch (IntentSender.SendIntentException e) {
                        }
                        break;
                    caseLocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                        break;
                }
            }
        });
    }

}

@OverridepublicvoidonConnectionSuspended(int i) {

}

@OverridepublicvoidonConnectionFailed(@NonNull ConnectionResult connectionResult) {
    DialogPopup.getInstance().showLocationSettingsAlert(context);
}

publicvoidremoveLocationUpdates() {
   if (googleApiClient.isConnected()){
       LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
   }
}

publicvoiddisconnectGps() {
    googleApiClient.disconnect();
}


}

In onStart of your activity call this :

LocationFetcherlocationFetcher=newLocationFetcher(this);
locationFetcher.connectGps();

And in onStop use this:

locationFetcher.disconnectGps();

You will get the location updates in the onLocationChangedMethod of the class.

You have to add this dependency too:

compile'com.google.android.gms:play-services-location:10.2.1'

Hope this helps.

Post a Comment for "How To Get Real Time Current Location Update As We Walk"