Code example for LocationClient

Methods: getLastLocation, isConnected, requestLocationUpdates

1
        // Request for location updates 
        LocationRequest request = LocationRequest.create()
                .setInterval(TWENTY_SECONDS_INTERVAL)
                .setExpirationDuration(THREE_MINUTES)
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationClient.requestLocationUpdates(request, internalLocationListener);
    } 
 
    private LocationListener getNewInternalLocationListener() {
        return new LocationListener() {
            @Override 
            public void onLocationChanged(Location location) {
                if (currentBestLocation == null) {
                    currentBestLocation = location;
                } 
                if (isBetterLocation(location, currentBestLocation)) {
                    currentBestLocation = location;
                    locationListener.onLocationChanged(location);
                } 
                if (currentBestLocation.getAccuracy() < 30) {
                    stopRequestingLocationUpdates(); 
                } 
            } 
        }; 
    } 
 
    private void stopRequestingLocationUpdates() { 
        Log.v(TAG, "Accuracy is very precise");
        if (stopLocationRequestsTimer != null) {
            stopLocationRequestsTimer.cancel();
        } 
        locationClient.disconnect();
        locationRequestsAreInProgress = false;
    } 
 
    /** 
     * Determines whether one Location reading is better than the current Location fix 
     * 
     * @param location            The new Location that you want to evaluate 
     * @param currentBestLocation The current Location fix, to which you want to compare the new one 
     */ 
    protected boolean isBetterLocation(Location location, Location currentBestLocation) {
        if (currentBestLocation == null) {
            // A new location is always better than no location 
            return true; 
        } 
 
        // Check whether the new location fix is newer or older 
        long timeDelta = location.getTime() - currentBestLocation.getTime();
        boolean isSignificantlyNewer = timeDelta > FIVE_SECONDS;
        boolean isSignificantlyOlder = timeDelta < -FIVE_SECONDS;
        boolean isNewer = timeDelta > 0;
 
        // If it's been more than two minutes since the current location, use the new location 
        // because the user has likely moved 
        if (isSignificantlyNewer) {
            return true; 
            // If the new location is more than two minutes older, it must be worse 
        } else if (isSignificantlyOlder) {
            return false; 
        } 
 
        // Check whether the new location fix is more or less accurate 
        int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
        boolean isLessAccurate = accuracyDelta > 0;
        boolean isMoreAccurate = accuracyDelta < 0;
        boolean isSignificantlyLessAccurate = accuracyDelta > 200;
 
        // Check if the old and new location are from the same provider 
        boolean isFromSameProvider = isSameProvider(location.getProvider(),
                currentBestLocation.getProvider());
 
        // Determine location quality using a combination of timeliness and accuracy 
        if (isMoreAccurate) {
            return true; 
        } else if (isNewer && !isLessAccurate) {
            return true; 
        } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
            return true; 
        } 
        return false; 
    } 
 
    /** 
     * Checks whether two providers are the same 
     */ 
    private boolean isSameProvider(String provider1, String provider2) {
        if (provider1 == null) {
            return provider2 == null;
        } 
        return provider1.equals(provider2);
    } 
 
    private class LocationClientCallbacksListener implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener {
 
        @Override 
        public void onConnected(Bundle connectionHint) {
            try { 
                Log.v(TAG, "Location Client connected");
                // Display last location 
                Location location = locationClient.getLastLocation();
                if (location != null) {
                    internalLocationListener.onLocationChanged(location);
                } 
                if (locationClient.isConnected()) {
                    requestLocationUpdates(); 
                } 
            } catch (IllegalStateException e) {
                Log.e(TAG, "Location can not be achieved. trying to reconnect");
                Handler handler = new Handler();