Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • How to get current location of an android device using Google Location Sevices API.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 522
    Comment on it

    The Google Location Services API, part of Google Play services, is the preferred way to add location-awareness to an android app.

    Follow the below steps to get current position of an android device.

    1. To use the location manager make the Google play service available via your app level build.gradle file.

    compile ''

    2. Add required permission in your AndroidManifest.xml file.

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

    3. Now, create a class.

    public class LocationManager implements
            LocationListener {
        private final String TAG = "LocationManager";
        private GoogleApiClient mGoogleApiClient;
        private LocationRequest mLocationRequest;
        private static Context context;
        private LocationManager() {
        private static LocationManager manager;
        public static LocationManager getInstance(Context _context) {
            if (manager == null) {
                manager = new LocationManager();
            context = _context;
            return manager;
        public void setUpLocation() {
                mGoogleApiClient = new GoogleApiClient.Builder(context)
        public void onConnected(Bundle bundle) {
        private void startPeriodicUpdates() {
            mLocationRequest = LocationRequest.create();
            mLocationRequest.setInterval(10000); // Update location every second
            try {
                        mGoogleApiClient, mLocationRequest, this);
            }catch (SecurityException e){
         * In response to a request to stop updates, send a request to
         * Location Services
        private void stopPeriodicUpdates() {
            if (mGoogleApiClient.isConnected())
                LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        public void stopLocationUpdate() {
        public boolean servicesConnected() {
            // Check that Google Play services is available
            GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
            int resultCode = googleAPI.isGooglePlayServicesAvailable(context);
            if (ConnectionResult.SUCCESS == resultCode) {
                // In debug mode, log the status
                // Continue
                return true;
             // Google Play services was not available for some reason
            } else {
                // Display an error dialog
                Dialog dialog = googleAPI.getErrorDialog((Activity) context,resultCode, 0);
                if (dialog != null) {
                return false;
        public void onConnectionSuspended(int i) {
            Log.i(TAG, "GoogleApiClient connection has been suspend");
        public void onConnectionFailed(ConnectionResult connectionResult) {
            Log.i(TAG, "GoogleApiClient connection has failed");
            ((LocationFound) context).locationFailed();
        public void onLocationChanged(Location location) {
            ((LocationFound) context).locationFound(location);
        public interface LocationFound {
            public void locationFound(Location location);
            public void locationFailed();

    4. Now in our activity class create an instance of LocationManager class and call setUpLocation().

    package com.realmdemo.activity;
    import android.location.Location;
    import android.os.Bundle;
    import android.util.Log;
    import com.realmdemo.R;
    import com.realmdemo.utility.LocationManager;
    public class LocationActivity extends AppCompatActivity implements LocationManager.LocationFound {
        private final String TAG = "LocationActivity";
        LocationManager locationManger=null;
        Location myLocation=null;
        protected void onCreate(Bundle savedInstanceState) {
        public void locationFound(Location location) {
            // TODO Auto-generated method stub
            Log.i(TAG, myLocation.getLatitude() + "----" + myLocation.getLongitude());
        public void locationFailed() {

    Thanks you.

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: