Android Get Current Address Location From Latitude Longitude | City

Learn about Android Get Current Address Location From Latitude Longitude in this tutorial.

In this tutorial, we will get current country, state, city, postal or pin code, zip code and location name from latitude and longitude.

For this purpose, we will use Geocoder and Address classes from android library.

Step 1. Manifest Permissions

First of all, make a fresh new project in the android studio.

Select “Empty activity” as a main activity.

In order to fetch the current location, we need to have some permissions from the user.

Write down the below lines in AndroidManifest.xml file.

  • We need to ask for runtime permissions in this case because location permission is sensitive in terms of user’s security and privacy.
  • We will ask it using dexter library in the main activity later.

Step 2. Some Dependencies

In your build.gradle(Module : app) file, add the following two lines

  • First line is for fetching the classes from google. These classes will help us to get the current location in terms of  latitude and longitude of the user.
  • Second line is for dexter library which will help us to simplify the process of asking runtime location permissions.

Step 3. Main XML file

In your activity_main.xml file, add the following code snippet

  • Above main XML file includes one Button and several Text views.
  • When the user will click the button, we will fetch the address from current latitude and longitude.
  • We will fill the text views with the information like country, city, state, pin code, zip code etc.

Step 4. Main Java Class

Following is the source code for the class.

Checking the Code

Let us see how the source code of Main Activity will work.

First of all, consider the following lines

  • With this code, compiler will define objects of various useful classes.
  • Classes like GoogleApiClient, Location, LocationManager, LocationRequest etc. will help us to get the current latitude and longitude.
  • Then there are objects of text view and button widgets.
  • Two double variables latitude and longitude will hold the co ordinates.
  • Now read the code of the onCreate() method.
  • First of all, compiler will call requestMultiplePermissions() method.

Code for requestMultiplePermissions() method is as the below

  • As you can see that we are asking for ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION in this method.
  • If the user have given all the permissions then compiler will show a toast like “All permissions are granted!”
  • Otherwise, it will call the openSettingDialog() method.

source code for openSettingDialog() method is as the following

  • Compiler will show one alert dialog to the user.
  • It has two buttons. One is Cancel and another is “Take me to settings.”
  • when the user will call “Take me to settings.” button, compiler will open the app settings, from where user can turn on the permissions.
  • Now again see the onCreate() method.
  • After requestMultiplePermissions() method, compiler will call checkLocation() method.
  • This method will check whether the location service of android device is on or off.
  • If it is off then, compiler will pop up one alert dialog and will tell user to turn on the location service.
  • Now some overriden methods are there. For example, onConnected(), onConnectionSuspendend(), onConnectionFailed(), onStart(), onStop() and onLocationChanged().
  • All these methods are related to getting current location.
  • Compiler will call onLocationChanged() method every 2 seconds. This method will give us updated latitude and longitudes.

Source code for onLocationChanged() method is as the following

  • Compiler will store latitude and longitude in global variables.
  • When the user will click the button, compiler will call setAddress() method.
  • setAddress() method will get two parameters. One latitude and another will longitude.
  • We will pass global variables as these parameter, so this method will always get updated current co ordinates.

Code structure for setAddress() method is as the below

  • In this method, compiler will first create the object of Geocoder class.
  • Then it will create a list with the objects of Address class.
  • .getFromLocation() method will give us the address associated with the latitude and longitude.
  • Result given by .getFromLocation() method will give us full address, city, country, state, pin code and finally we will set these information in various text views.

Download Code for Android Get Current Address

Download Source code for Address Current Location

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

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