Get Current Location Android full source code

In build.gradle(app)


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

In Android Manifest,


<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

Layout (get_location.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"    
xmlns:tools="http://schemas.android.com/tools"    
android:id="@+id/activity_main"    
android:layout_width="match_parent"    
android:layout_height="match_parent"    
android:gravity="center"    
android:orientation="vertical"    
android:padding="8dp"    >

<TextView  
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="18dp"
android:layout_marginTop="12dp"
android:text="You Location is:"        
android:textSize="24sp" />

<TextView        
android:id="@+id/longitude"        
android:layout_width="wrap_content"        
android:layout_height="wrap_content" />

<TextView        
android:id="@+id/latitude"        
android:layout_width="wrap_content"        
android:layout_height="wrap_content" />

<LinearLayout        
android:layout_width="match_parent"        
android:layout_height="match_parent"        
android:gravity="center">

<ProgressBar            
android:id="@+id/progressBar"            
style="?android:attr/progressBarStyleLarge"            
android:layout_width="wrap_content"            
android:layout_height="wrap_content" />
</LinearLayout>

</LinearLayout>


Java Source Code(.java)

import android.support.v7.app.AppCompatActivity;

/** * Created by Sweets-Cutes on 08-09-2018. */
import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender;
import android.content.IntentSender.SendIntentException;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;

import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsResult;
import com.google.android.gms.location.LocationSettingsStates;
import com.google.android.gms.location.LocationSettingsStatusCodes;

import static com.google.android.gms.common.api.GoogleApiClient.*;

public class GetLocation extends AppCompatActivity implements        ConnectionCallbacks, OnConnectionFailedListener, LocationListener {

    TextView _latitude, _longitude;
    ProgressBar _progressBar;
    GoogleApiClient mGoogleApiClient;
    Location mLastLocation;
    LocationRequest mLocationRequest;


    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.get_location);

        _latitude = (TextView) findViewById(R.id.latitude);
        _longitude = (TextView) findViewById(R.id.longitude);
        _progressBar = (ProgressBar) findViewById(R.id.progressBar);


        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();

        if (mGoogleApiClient != null) {
            mGoogleApiClient.connect();
        } else            Toast.makeText(this, "Not Connected!", Toast.LENGTH_SHORT).show();

    }

    /*Ending the updates for the location service*/    @Override    protected void onStop() {
        mGoogleApiClient.disconnect();
        super.onStop();
    }

    @Override    public void onConnected(@Nullable Bundle bundle) {
        settingRequest();
    }

    @Override    public void onConnectionSuspended(int i) {
        Toast.makeText(this, "Connection Suspended!", Toast.LENGTH_SHORT).show();
    }

    @Override    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        Toast.makeText(this, "Connection Failed!", Toast.LENGTH_SHORT).show();
        if (connectionResult.hasResolution()) {
            try {
                // Start an Activity that tries to resolve the error                connectionResult.startResolutionForResult(this, 90000);
            } catch (IntentSender.SendIntentException e) {
                e.printStackTrace();
            }
        } else {
            Log.i("Current Location", "Location services connection failed with code " + connectionResult.getErrorCode());
        }
    }

    /*Method to get the enable location settings dialog*/    public void settingRequest() {
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(10000);    // 10 seconds, in milliseconds        mLocationRequest.setFastestInterval(1000);   // 1 second, in milliseconds        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                .addLocationRequest(mLocationRequest);

        PendingResult result =
                LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient,
                        builder.build());

        result.setResultCallback(new ResultCallback() {

            @Override            public void onResult(@NonNull LocationSettingsResult result) {
                final Status status = result.getStatus();
                final LocationSettingsStates state = result.getLocationSettingsStates();
                switch (status.getStatusCode()) {
                    case LocationSettingsStatusCodes.SUCCESS:
                        // All location settings are satisfied. The client can                        // initialize location requests here.                        getLocation();
                        break;
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        // Location settings are not satisfied, but this can be fixed                        // by showing the user a dialog.                        try {
                            // Show the dialog by calling startResolutionForResult(),                            // and check the result in onActivityResult().                            status.startResolutionForResult(GetLocation.this, 1000);
                        } catch (SendIntentException e) {
                            // Ignore the error.                        }
                        break;
                    case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                        // Location settings are not satisfied. However, we have no way                        // to fix the settings so we won't show the dialog.                        break;
                }
            }

        });
    }

    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
        switch (requestCode) {
            case 1000:
                switch (resultCode) {
                    case Activity.RESULT_OK:
                        // All required changes were successfully made                        getLocation();
                        break;
                    case Activity.RESULT_CANCELED:
                        // The user was asked to change settings, but chose not to                        Toast.makeText(this, "Location Service not Enabled", Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        break;
                }
                break;
        }
    }

    public void getLocation() {
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling            //    ActivityCompat#requestPermissions            // here to request the missing permissions, and then overriding            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,            //                                          int[] grantResults)            // to handle the case where the user grants the permission. See the documentation            // for ActivityCompat#requestPermissions for more details.            return;
        } else {
            /*Getting the location after aquiring location service*/            mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                    mGoogleApiClient);

            if (mLastLocation != null) {
                _progressBar.setVisibility(View.INVISIBLE);
                _latitude.setText("Latitude: " + String.valueOf(mLastLocation.getLatitude()));
                _longitude.setText("Longitude: " + String.valueOf(mLastLocation.getLongitude()));
            } else {
                /*if there is no last known location. Which means the device has no data for the loction currently.                * So we will get the current location.                * For this we'll implement Location Listener and override onLocationChanged*/                Log.i("Current Location", "No data for location found");

                if (!mGoogleApiClient.isConnected())
                    mGoogleApiClient.connect();

                LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, GetLocation.this);
            }
        }
    }

    /*When Location changes, this method get called. */    @Override    public void onLocationChanged(Location location) {
        mLastLocation = location;
        _progressBar.setVisibility(View.INVISIBLE);
        _latitude.setText("Latitude: " + String.valueOf(mLastLocation.getLatitude()));
        _longitude.setText("Longitude: " + String.valueOf(mLastLocation.getLongitude()));
    }
}


Comments