Android Get Address From Latitude And Longitude | Get City Name

Welcome to Android Get Address From Latitude And Longitude tutorial example.

You will learn get country, city name , postal code, pin code or zip code from latitude and longitude of any location.

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

Geocoder class will get the latitude and longitude of any place as an input and gives us the street address of that location as an output.

For more exposure, we will get the address from three different countries.

We will make one spinner, which has three different places and user will select place from this spinner.

After the selection, we will fetch the address of that selected place.

Look at below address

Load the below video to see the output of this tutorial example.

Now follow all the below steps to make above example.

Step 1. Permission of Internet

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

While making a new project, select “Empty activity” as a template.

Now in the AndroidManifest.xml file, add the below line

So the final source code for AndroidManifest.xml file is looking like the following

Internet is considered as the safe permission for user’s privacy. Hence, we do not need to ask for runtime permission in this tutorial.

Step 2. Write XML file

In the activity_main.xml file, add the below code snippet

  • I have added one spinner at the top of the file.
  • User will have to select any place from this spinner. We will fetch the address of that selected place.
  • Other text views will hold the information like full address, country name, city name, state name and pin code or zip code.

Step 3. Main JAVA file

Source code for MainActivity.java file is like the following code structure.

Starring At Main Activity

Let us look more deeply in main activity and understand what every line will do.

First of all, consider the below code lines

  • First line will create one string array. This string array contains the name of the famous places like Taj Mahal, Eiffel Tower and Burj Khalifa.
  • We will set these place names in the spinner and user will select place from that spinner.
  • Second line is the double array variable. It holds the values of the latitudes of the places.
  • For example, it’s first value is the latitude of Taj Mahal, second is of Eiffel tower and last is of Burj Khalifa.
  • Similarly, third line is the double array with longitude values of the places in similar order.
  • Fourth line will create some objects of the text view class which will hold values like country, city pin code etc.
  • Last one is the object of the spinner class.

Now read the below code snippet

  • Using the first line, compiler will create the object of the array adapter.
  • Here, compiler will use the string array ( which include the names of the places ) as the data source.
  • Then second line will set the look and feel of the drop down menu for spinner.
  • Finally, last line will set the adapter to the spinner.

Now attend the below code

  • Compiler will execute the above code when the user clicks the spinner and selects any option.
  • Here, it will call the setTexts() method.
  • In the parameters of setTexts() method, compiler will put the selected position in an integer format.

Following are the coding lines for setTexts() method

  • At the staring of the method, compiler will create the object of Geocoder class and a list of Address class.
  • Then it will fetch the address using .getFromLocation() method.
  • Parameter for .getFromLocation() method are latitude and longitude of the place.
  • At this point, we have get the address from latitude and longitude.
  • Then compiler will get the full address, city, state, country, postal code and knownName in the string format.

And finally, set all these values into the various text views and we are ready for the app launch !

Download Code For Android Get Address From Latitude And Longitude

Download Source code for Address from LatLong

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 *