How to get Current Latitude and longitude in Android

642 views.

This Android tutorial is to help learn How to get current latitude and longitude in the Android platform. Knowing the current location in an android mobile will pave the way for developing many innovative Android apps to solve people’s daily problem. For developing the location-aware application in android, it needs location providers.
I have added coding for getting the current location in the device using Location Manager.
There are two types of location providers,
      1.GPS Location Provider
     2.  Network Location Provider

Anyone of the above providers is enough to get the current location of the user or user’s device. But, it is recommended to use both providers as they both have different advantages. Because GPS provider will take the time to get the location to the indoor area. And, the Network Location Provider will not get location when the network connectivity is poor.

Network Location Provider vs GPS Location Provider

  • Network Location provider is comparatively faster than the GPS provider in providing the location coordinates.
  • GPS provider may be very very slow in indoor locations and will drain the mobile battery.
  • Network location provider depends on the cell tower and will return our nearest tower location.

GPS location provider will give our location accurately.

Steps to get current latitude and longitude in Android

  1. Provide permissions for the manifest file for receiving the location update
  2. Create LocationManager instance as a reference to the location service
  3. Request location from LocationManager
  4. Receive location update from LocationListener on change of location
To access current location information through location providers, we need to set permissions with the android manifest file.

activity_main.xml

MainActivity.Java

LocationTrack.java

Output


You may also like...

Leave a Reply