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.

 

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

  1. Shama Furhana

    I’m studying 2nd yr cs engineering . will yu plz give some idea to do mini project in c,c++…..

    Reply
  2. Vinay A

    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

    Reply
    1. Admin Post author

      Makes sure the gps is one, test it in real android device.

      Reply
  3. Todd

    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!

    Reply

Leave a Reply

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