How to Get Current Location in Android Using Location Manager

In this GPS tutorial you will learn how to get current location in android.

LocationManager class provides the facility to get latitude and longitude coordinates of current location. The class in which you want to get location should implement LocationListener and override all its abstract methods.

How to Get Current Location in Android Using Location Manager

Add following permissions to AndroidManifest.xml file.

Following lines of code helps in getting current location.

In requestLocationUpdates() method the 2nd argument is time in milliseconds and 3rd argument is distance in meters. Here I have used 5000 and 5 that means after every 5 seconds and 5 meter the current location is fetched.

To get coordinates use getLatitude() and getLongitude() method on variable of Location class.

Whenever location is changed it can be fetched inside onLocationChanged() method.

Below is simple application that fetches current location on button click and displays it in textview.

Full Source Code

activity_main.xml

AndroidManifest.xml

MainActivity.java

Screenshot

How to Get Current Location in Android Using Location Manager

Comment below if found anything incorrect or have doubts related to above android gps tutorial to get current location.

 

27 thoughts on “How to Get Current Location in Android Using Location Manager”

    1. locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);

      it works if you add NETWORK_PROVIDER

  1. Hi,
    When i click the buton it show nothing lang and lat…but i add Toast between this,
    Toast.makeText(getApplicationContext(), “Your Location”, Toast.LENGTH_LONG).show();
    getLocation();
    Toast.makeText(getApplicationContext(), “Your Location showing”, Toast.LENGTH_LONG).show();

    But,Am able to find long and lat.
    Advance Thanks

  2. Hello Neeraj!

    I am running the tutorial code on a real device and gps is on but onLocationChanged is never called. I was wondering what version of the sdk you are running?

    Thanks!

    1. Mohammed Arshad

      Use This Code:

      if(checkLocationPermission()) {
      locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
      Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
      Log.e(“Current Location”,”lat: “+location.getLatitude()+” Lon: “+location.getLongitude());

      }

  3. Hi

    For this to work on my phone(Nougat). I needed to go to Settings, Location Services, App-Level Permission. My newly deployed app appeared in the list. I needed toggle the on button.
    Then go back to your app, press the button, wait a few seconds and the location appears

    Hope this helps.

    1. Thank you. This works. The problem at the above code is they are not asking for permission. You must give the permission manually.

  4. your code is not working
    just button is getting displayed
    when i clicked on that button (GET CURRENT LOCATION),nothing got displayed, i have already GPS on

  5. Saurabh Sawarkar

    In above code you displaying the current location of your.,
    But what if we want to find the location of other…
    can you code for that???

  6. Many Thanks sir,
    It is working fine…We have to add device permission dynamically for latest versions of android…

  7. Hi thank you for sharing. i need to get current location. but i click button i didn’t get any values.
    am trying 1 week but i can’t get any code. please help me.

  8. Hi, this code works accurately fine. This was not working on emulator. I installed this app in my mobile device and still it was not working. Then as Matt suggested above, I went to Settings of my mobile and typed Permissions in search box to access Permission Settings. There I opened App Permissions options and in App Permissions options I opened Your Location Permissions. There I saw all installed Apps of my device. I just turned on Your Location Permission for this app. Then I turned on GPS and ran the app. It worked.
    Also be careful about package name while pasting the above code in your code.
    Regards,

Leave a Comment

Your email address will not be published. Required fields are marked *