Tutorial

Android Location API to track your current location

Published on August 3, 2022
author

By Anupam Chugh

Android Location API to track your current location

Android Location API can be used to track your mobile current location and show in the app. In this tutorial, we’ll develop an application that fetches the user’s current location programmatically.

Android Location API

There are two ways to get a users location in our application:

  • android.location.LocationListener : This is a part of the Android API.
  • com.google.android.gms.location.LocationListener : This is present in the Google Play Services API. (We’ll look into this in the next tutorial)

Android Location Services is available since Android API 1. Google officially recommends using Google Play Location Service APIs. Android Location Services API is still used to develop location-based apps for devices that don’t support Google Play Services.

LocationListener

The LocationListener interface, which is part of the Android Locations API is used for receiving notifications from the LocationManager when the location has changed. The LocationManager class provides access to the systems location services. The LocationListener class needs to implement the following methods.

  • onLocationChanged(Location location) : Called when the location has changed.
  • onProviderDisabled(String provider) : Called when the provider is disabled by the user.
  • onProviderEnabled(String provider) : Called when the provider is enabled by the user.
  • onStatusChanged(String provider, int status, Bundle extras) : Called when the provider status changes.

The android.location has two means of acquiring location data:

  • LocationManager.GPS_PROVIDER: Determines location using satellites. Depending on the conditions, this provider may take a while to return a location fix
  • LocationManager.NETWORK_PROVIDER: Determines location based on the availability of nearby cell towers and WiFi access points. This is faster than GPS_PROVIDER

In this tutorial, we’ll create a Service that implements the LocationListener class to receive periodic location updates via GPS Providers or Network Providers.

Android Location API Project Structure

android location api, android gps location The project consists of a MainActivity.java class which displays a Get Location and a LocationTrack.java Service class.

Android Location APICode

The activity_main.xml layout is defined below.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.journaldev.gpslocationtracking.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn"
        android:layout_centerInParent="true"
        android:text="GET LOCATION" />
</RelativeLayout>

The MainActivity.java class is defined below.

package com.journaldev.gpslocationtracking;

import android.annotation.TargetApi;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;

import static android.Manifest.permission.ACCESS_COARSE_LOCATION;
import static android.Manifest.permission.ACCESS_FINE_LOCATION;

public class MainActivity extends AppCompatActivity {


    private ArrayList permissionsToRequest;
    private ArrayList permissionsRejected = new ArrayList();
    private ArrayList permissions = new ArrayList();

    private final static int ALL_PERMISSIONS_RESULT = 101;
    LocationTrack locationTrack;

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

        permissions.add(ACCESS_FINE_LOCATION);
        permissions.add(ACCESS_COARSE_LOCATION);

        permissionsToRequest = findUnAskedPermissions(permissions);
        //get the permissions we have asked for before but are not granted..
        //we will store this in a global list to access later.


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {


            if (permissionsToRequest.size() > 0)
                requestPermissions(permissionsToRequest.toArray(new String[permissionsToRequest.size()]), ALL_PERMISSIONS_RESULT);
        }


        Button btn = (Button) findViewById(R.id.btn);


        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                locationTrack = new LocationTrack(MainActivity.this);


                if (locationTrack.canGetLocation()) {


                    double longitude = locationTrack.getLongitude();
                    double latitude = locationTrack.getLatitude();

                    Toast.makeText(getApplicationContext(), "Longitude:" + Double.toString(longitude) + "\nLatitude:" + Double.toString(latitude), Toast.LENGTH_SHORT).show();
                } else {

                    locationTrack.showSettingsAlert();
                }

            }
        });

    }


    private ArrayList findUnAskedPermissions(ArrayList wanted) {
        ArrayList result = new ArrayList();

        for (String perm : wanted) {
            if (!hasPermission(perm)) {
                result.add(perm);
            }
        }

        return result;
    }

    private boolean hasPermission(String permission) {
        if (canMakeSmores()) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                return (checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED);
            }
        }
        return true;
    }

    private boolean canMakeSmores() {
        return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);
    }


    @TargetApi(Build.VERSION_CODES.M)
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

        switch (requestCode) {

            case ALL_PERMISSIONS_RESULT:
                for (String perms : permissionsToRequest) {
                    if (!hasPermission(perms)) {
                        permissionsRejected.add(perms);
                    } 
                }

                if (permissionsRejected.size() > 0) {


                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        if (shouldShowRequestPermissionRationale(permissionsRejected.get(0))) {
                            showMessageOKCancel("These permissions are mandatory for the application. Please allow access.",
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                                requestPermissions(permissionsRejected.toArray(new String[permissionsRejected.size()]), ALL_PERMISSIONS_RESULT);
                                            }
                                        }
                                    });
                            return;
                        }
                    }

                }

                break;
        }

    }

    private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
        new AlertDialog.Builder(MainActivity.this)
                .setMessage(message)
                .setPositiveButton("OK", okListener)
                .setNegativeButton("Cancel", null)
                .create()
                .show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        locationTrack.stopListener();
    }
}

In the above code, we’re implementing runtime permissions that are used in Android 6.0+ devices. We’ve added the ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions in the AndroidManifest.xml file. Clicking the button invokes the LocationTrack.java service class. If the location returned is NULL in the case of GPS Provider, we call the showSettingsAlert() method from the LocationTrack.java class that we’ll be seeing shortly. When the activity is destroyed stopLocationTrack() method is called to turn off the location updates. The LocationTrack.java class is defined below.

public class LocationTrack extends Service implements LocationListener {

    private final Context mContext;


    boolean checkGPS = false;


    boolean checkNetwork = false;

    boolean canGetLocation = false;

    Location loc;
    double latitude;
    double longitude;


    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;


    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;
    protected LocationManager locationManager;

    public LocationTrack(Context mContext) {
        this.mContext = mContext;
        getLocation();
    }

    private Location getLocation() {

        try {
            locationManager = (LocationManager) mContext
                    .getSystemService(LOCATION_SERVICE);

            // get GPS status
            checkGPS = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);

            // get network provider status
            checkNetwork = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!checkGPS && !checkNetwork) {
                Toast.makeText(mContext, "No Service Provider is available", Toast.LENGTH_SHORT).show();
            } else {
                this.canGetLocation = true;

                // if GPS Enabled get lat/long using GPS Services
                if (checkGPS) {

                    if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, 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.
                    }
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    if (locationManager != null) {
                        loc = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (loc != null) {
                            latitude = loc.getLatitude();
                            longitude = loc.getLongitude();
                        }
                    }


                }


                /*if (checkNetwork) {


                    if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, 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.
                    }
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                    if (locationManager != null) {
                        loc = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                    }

                    if (loc != null) {
                        latitude = loc.getLatitude();
                        longitude = loc.getLongitude();
                    }
                }*/

            }


        } catch (Exception e) {
            e.printStackTrace();
        }

        return loc;
    }

    public double getLongitude() {
        if (loc != null) {
            longitude = loc.getLongitude();
        }
        return longitude;
    }

    public double getLatitude() {
        if (loc != null) {
            latitude = loc.getLatitude();
        }
        return latitude;
    }

    public boolean canGetLocation() {
        return this.canGetLocation;
    }

    public void showSettingsAlert() {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);


        alertDialog.setTitle("GPS is not Enabled!");

        alertDialog.setMessage("Do you want to turn on GPS?");


        alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                mContext.startActivity(intent);
            }
        });


        alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });


        alertDialog.show();
    }


    public void stopListener() {
        if (locationManager != null) {

            if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, 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;
            }
            locationManager.removeUpdates(LocationTrack.this);
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onLocationChanged(Location location) {

    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

    }
}

Few inferences drawn from the above code are:

  • In the above code isProviderEnabled(String provider) is called upon the locationManager object and is used to check whether GPS/Network Provider is enabled or not.

  • If the Providers aren’t enabled we’re calling the method showSettingsAlert() that shows a prompt to enable GPS.

  • requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener) method of the LocationManager class is used to register the current activity to be notified periodically by the named provider.

  • onLocationChanged is invoked periodically based upon the minTime and minDistance, whichever comes first.

  • Location class hosts the latitude and longitude. To get the current location the following code snippet is used.

    Location loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    

    On the above Location object, getters are called to store the double values of latitude and longitude. These double values are then disabled as a Toast message on the screen.

  • To stop location updates removeUpdates method is called on the LocationManager instance.

The output of the above application in action on an emulator is: android location api, android location tracking Our emulators can’t fetch locations, hence it’s returning 0.0 for lat/lng. You can connect your smartphone and run the application in debug mode to check your current location. To emulate GPS Locations in an emulator we can pass fixed Latitude and Longitude values from Android Studio. Besides the emulator window, you can see a list of options. The one at the bottom (which has three dots) is our Extended Controls options. Open that and send a dummy location. Your application should look like this: android location manager This brings an end to this tutorial. We’ll be implementing Location API using Google Play Services in a later tutorial. You can download the Android GPSLocationTracking Project from the link below.

Download Android Location API Project

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Category:
Tutorial
Tags:

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
August 20, 2017

Hi , I am getting errors in the MainActivity 1. requestPermissions(permissionsToRequest.toArray(new String[permissionsToRequest.size()]), ALL_PERMISSIONS_RESULT); It says wrong first argument type. 2.for (String perms : permissionsToRequest) Incompatible types: Required object found String

- Sana Baig

JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
August 21, 2017

Hi Sana, Use : private ArrayList<String> permissionsToRequest; private ArrayList<String> permissionsRejected = new ArrayList<>(); private ArrayList<String> permissions = new ArrayList<>(); Instead of : private ArrayList permissionsToRequest; private ArrayList permissionsRejected = new ArrayList(); private ArrayList permissions = new ArrayList(); Thanks

- Anupam Chugh

JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
August 21, 2017

Thanks Anupam, I am able to run the app without any errors but the coordinates are coming to 0,0 on my smartphone. I have enabled location on my phone. Can you help me understand what the issue could be?

- Sana Baig

JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
August 22, 2017

Open Google Maps app and click button on map to get current location. Now come back to your application and try to get current location and it will work.

- Anupam Chugh

JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
August 23, 2017

It still shows 0,0 after clicking map on google map. What to do now?

- Alam

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    September 7, 2017

    yes, Even for me the location is still coming up as 0,0

    - Sana Baig

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      September 7, 2017

      Its working for me now… i just debugged the code and found that i am getting loc as null in the below snippet loc = locationManager .getLastKnownLocation(LocationManager.GPS_PROVIDER); i tried uncommenting the below snippet in the LocationTrack Class /*if (checkNetwork) { if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, 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. } locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); if (locationManager != null) { loc = locationManager .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); } if (loc != null) { latitude = loc.getLatitude(); longitude = loc.getLongitude(); } }*/

      - Sana Baig

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        September 4, 2017

        sir this code run and show current location in my genymotion device but i install app in my mobile but problem is that latiude and longitude take 0.0 i dont know why this 0.0 please help me sir what i do?

        - abhishek phalke

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          January 3, 2018

          Sir I want to send these latitude and longitude to the server(on a defined time using alarm manager) and need to compare these values with the location values already given on the server. how can i achieve this ???please help me to achieve that

          - Gunjan Sharma

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          November 20, 2019

          Did you get a answer for this?

          - rahul

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            February 22, 2018

            Not Working!! Gives latlng 0.0

            - Rishav

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              April 6, 2018

              still not working

              - Sandeep Padhi

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                May 9, 2018

                Hi. May I know why you are save the permissions in array list??

                - Vijay

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  June 7, 2018

                  my gps is on still it gives the dialog box please help

                  - rahul patel

                    JournalDev
                    DigitalOcean Employee
                    DigitalOcean Employee badge
                    June 18, 2018

                    hi sir, how to get the latlng without using gps and can i get the latlng values using mobile networks .can you upload the sample code

                    - nikhil

                      JournalDev
                      DigitalOcean Employee
                      DigitalOcean Employee badge
                      August 22, 2018

                      showing no location 0.0

                      - Dheeraj Vaid

                      JournalDev
                      DigitalOcean Employee
                      DigitalOcean Employee badge
                      March 26, 2019

                      Its due to the fact the App is being run on an emulator, whose default Location values are set to Coordinates (0,0). You can use the options of the emulator and set a particular location, then that will reflect in the output. The Same won’t happen in a Real Device, where the GPS Radio has the Previously Known Location Details obtained from a Location Provider.

                      - Sri Krishna

                        JournalDev
                        DigitalOcean Employee
                        DigitalOcean Employee badge
                        October 26, 2018

                        How to get the latitude and longitude of desired location.?

                        - Abhilash Karanth

                          JournalDev
                          DigitalOcean Employee
                          DigitalOcean Employee badge
                          November 8, 2018

                          I am getting my current location in Nougat 7.1.1(API level is 25) but i Can’t get current location in Oreo 8.1(API level is 27). compileSdkVersion and targetSdkVersion is 27. Please give suggestions… Thanks in advance

                          - Abhilash Karanth

                            JournalDev
                            DigitalOcean Employee
                            DigitalOcean Employee badge
                            December 6, 2018

                            I want to create background services for android to get the user location, SMS, call logs, contacts etc. on my desktop. How to do that?

                            - shubhi

                            JournalDev
                            DigitalOcean Employee
                            DigitalOcean Employee badge
                            October 14, 2021

                            isn’t it will be kind of hacking? lol

                            - Akash

                              JournalDev
                              DigitalOcean Employee
                              DigitalOcean Employee badge
                              December 7, 2018

                              Hi… I am getting location 0.0 at my smart phone. loc = locationManager .getLastKnownLocation(LocationManager.GPS_PROVIDER); loc is always null, i tried by getting last location from google map as well. Thanks for the code.

                              - Saim Anwer

                              JournalDev
                              DigitalOcean Employee
                              DigitalOcean Employee badge
                              September 30, 2019

                              Sir your comment box requires a margin of ‘-5px’ which was ‘-15px’. This is because whenever a new line gets added in the comment section, the ‘Reply’ gets overlapped on the comments. Also, there is no difference between small alphabet ‘i’ and ‘l’. Thank you. A Great Website to learn for Beginners!. :)

                              - Shani Maurya

                                JournalDev
                                DigitalOcean Employee
                                DigitalOcean Employee badge
                                December 18, 2018

                                This app displays the current location whenever the button is pressed, what if I want to display and update the current location in a textview. By just setting the text for the textview in the onCreate() method, does not work. Tried to do it using BroadcastManager, didn’t manage to make it work. Do you have any examples for this, if not, can you make a tutorial for this?

                                - Alex P.

                                  JournalDev
                                  DigitalOcean Employee
                                  DigitalOcean Employee badge
                                  May 25, 2019

                                  Your tutorial is great. One thing you need to fix on your site is that the tutorial menu of your site is being covered by Ads view when your website is being opened in mobile device. This can be in some mobile device. Just to let you know that needs your attention. Thanks

                                  - Jay

                                  JournalDev
                                  DigitalOcean Employee
                                  DigitalOcean Employee badge
                                  May 25, 2019

                                  Check out our android application which is available on play store in the mean time!

                                  - Anupam Chugh

                                    JournalDev
                                    DigitalOcean Employee
                                    DigitalOcean Employee badge
                                    August 19, 2019

                                    hi, When my app is in background i did’t recieve geo position can you help me

                                    - Ashu

                                      JournalDev
                                      DigitalOcean Employee
                                      DigitalOcean Employee badge
                                      October 11, 2019

                                      java.lang.RuntimeException at android.location.LocationManager.requestLocationUpdates(LocationManager.java:1013) at android.location.LocationManager.requestLocationUpdates(LocationManager.java:562) at com.journaldev.gpslocationtracking.LocationTrack.getLocation(LocationTrack.java:82) at com.journaldev.gpslocationtracking.LocationTrack.(LocationTrack.java:48) at com.journaldev.gpslocationtracking.MainActivity$1.onClick(MainActivity.java:58) at android.view.View.performClick(View.java:6608) at android.view.View.performClickInternal(View.java:6585) at android.view.View.access$3100(View.java:782) at android.view.View$PerformClick.run(View.java:25945) at android.os.Handler.handleCallback(Handler.java:874) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:198) at android.app.ActivityThread.main(ActivityThread.java:6729) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

                                      - Karthik

                                        JournalDev
                                        DigitalOcean Employee
                                        DigitalOcean Employee badge
                                        January 16, 2020

                                        Hey Anupam Chugh, I’m getting lat 0.0,long 0.0 can you look into it.

                                        - Chethan

                                        JournalDev
                                        DigitalOcean Employee
                                        DigitalOcean Employee badge
                                        February 1, 2020

                                        I want the code in java that will allow any app to get unlocked when that device will reach in the specific premises . and the area should be restricted to the specific area

                                        - Muskan Mishra

                                          JournalDev
                                          DigitalOcean Employee
                                          DigitalOcean Employee badge
                                          February 28, 2021

                                          add user permissions in the manifest

                                          - sashang s

                                            JournalDev
                                            DigitalOcean Employee
                                            DigitalOcean Employee badge
                                            February 10, 2020

                                            Hi Anupam Ur code was nice I want to get gps location automatically in time interval until I close r click a button like stop,and also is there a way to upload it to google sheets.

                                            - Shyam

                                              JournalDev
                                              DigitalOcean Employee
                                              DigitalOcean Employee badge
                                              April 15, 2020

                                              In Android version 10 Location tracking not working and i am using transport tracker googleapi for getting the latlong to tracking the location. please kindly help me to resolve from this .

                                              - pugazenthi

                                              JournalDev
                                              DigitalOcean Employee
                                              DigitalOcean Employee badge
                                              May 28, 2020

                                              LocationTrack locationtrack not working why

                                              - mohammed rashid kp rashid kp

                                                JournalDev
                                                DigitalOcean Employee
                                                DigitalOcean Employee badge
                                                June 28, 2020

                                                Hello sir.is there any program which pick the current address of user with latitude and longnitude.

                                                - abdul moiz

                                                  JournalDev
                                                  DigitalOcean Employee
                                                  DigitalOcean Employee badge
                                                  August 6, 2020

                                                  Hi, I’m always getting lat and lon 0.0, even on real devices. Any ideas with that?

                                                  - Sam Zhang

                                                    JournalDev
                                                    DigitalOcean Employee
                                                    DigitalOcean Employee badge
                                                    August 26, 2020

                                                    What about the commented code? You are only using GPS service to get the location. For a real application, the commented lines should be uncommented and before the GPS part?

                                                    - Jaime

                                                      JournalDev
                                                      DigitalOcean Employee
                                                      DigitalOcean Employee badge
                                                      January 17, 2021

                                                      Youor code work but real device not show logiture/latiture?

                                                      - satish

                                                        JournalDev
                                                        DigitalOcean Employee
                                                        DigitalOcean Employee badge
                                                        January 17, 2021

                                                        sir can i get android code for friends location logi/lati?

                                                        - satish

                                                          JournalDev
                                                          DigitalOcean Employee
                                                          DigitalOcean Employee badge
                                                          February 12, 2021

                                                          This stuff works very fine for me. I add this into a timer instead of a button click, so that i can control when to get the location coordinates to be pass into google mapviewer in real time.

                                                          - amado tabasa

                                                          JournalDev
                                                          DigitalOcean Employee
                                                          DigitalOcean Employee badge
                                                          February 16, 2021

                                                          Is using this location app devoloped in android studio will be charged if I run the apk in other real phone.please suggest on it .

                                                          - RUBINA

                                                            Join the Tech Talk
                                                            Success! Thank you! Please check your email for further details.

                                                            Please complete your information!

                                                            Become a contributor for community

                                                            Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                                                            DigitalOcean Documentation

                                                            Full documentation for every DigitalOcean product.

                                                            Resources for startups and SMBs

                                                            The Wave has everything you need to know about building a business, from raising funding to marketing your product.

                                                            Get our newsletter

                                                            Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

                                                            New accounts only. By submitting your email you agree to our Privacy Policy

                                                            The developer cloud

                                                            Scale up as you grow — whether you're running one virtual machine or ten thousand.

                                                            Get started for free

                                                            Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

                                                            *This promotional offer applies to new accounts only.