How to get current GPS location in Android using android Studio

0 1,982

Get current GPS location in Android

In this Android tutorial, we will learn how to Get current GPS location in Android using the LocationManager and LocationListener.

GPS (Global Positioning System), is a radio navigation system which allows land, sea, and airborne users to verify their exact location, velocity, and time 24 hours a day, in every weather conditions, anywhere in the world. In order to know where the users are allowed your App to be smarter and deliver better information to the user. When we develop a location-aware application for Android, GPS is most accurate. It specifically works for outdoors.
We need to integrate location modules in your Application to use GPS. To get user location in Android works by means of callback. The two types of Location modules are given below.

LocationManger

This class provides access to the system location services. You can receive location updates from the LocationManger by calling requestLocationUpdates() and pass it a LocationListener.

LocationListener

Used for receiving notifications from the LocationManger, when the location changes. We call these methods if the LocationListener has been registered with the LocationManger service using requestLocationUpdates(String, long, float, LocationListener) method.
Now let me give you an example to add GPS location in Android.

Steps to Follow

  1. Create a New Android Studio Project and name it then Press Next.
  2. Set the minimum SDK as API 21 and Android 4.1(Jelly Bean). As lower API levels target more devices.
  3. Create a Blank Activity and name it as MainActivity and press Finish.
  4. Write the following code in AndroidManifest.xml, inside the manifests folder.

5. Write the following code inside MainActivity.java.

6.Write the following code inside activity_main.xml.

7.Use emulator to get the current user’s location (longitudes and latitudes).
8.Before running and testing the App, we need to enable the GPS option.
9.Now you can run your Application and you would get  location coordinates.

Comments

comments

Leave A Reply

Your email address will not be published.