Android Get Latitude And Longitude From Address Of Location/Place

Android Get Latitude And Longitude From Address is the today’s main topic.

We will fetch the co ordinates in form of latitude and longitude from the address of the location.

There may be some case where user do not know his/her current location in terms of latitude and longitude but he can write his address.

In such case, you need to fetch the co ordinates from the address given by the user.

This tutorial will guide you how to insert this feature in your android application with easy and simple coding lines.

User will enter the address in the edit box. Our code will get the co ordinates (latitude and longitude) from this address using Address, Geocoder and Message class.

Read Fetched Address

Watch the below video to show how this demo tutorial is working.

Step 1. Geo Coding Class

First of all, make a new class named

Source code for is as the following

Above class is primarily using three classes : Geocoder, Address and Message.

Compiler will first start one thread.

Inside this thread, it will create the object of the Geocoder class.

Then it will fetch the latitude and longitude using .getFromLocationName() method.

An object of List class will hold the output given by .getFromLocationName() method. Object of Address class will get output from List class object.

After this, an object of StringBuilder class will fetch co ordinates and than a string variable will finally get latitude and longitude from given address.

Step 2. XML file for main activity

When you create a new project in android studio, system creates two files automatically.

One of them is activity_main.xml.

Copy the below code snippet in activity_main.xml.

There are some UI widgets in the above file.

Two buttons, two text views and one edit text is present.

In the edit text, user can input the address. By default, I have written one address.

Below this edit text, there will be two buttons. One button will erase the text from the edit text when user clicks it.

Other button will fetch the latitude and longitude from the address when it is clicked.

Step 3. Writing Main File

Write down the below source code in file

Looking Main Class Deeply

Consider the below code

Compiler will create the objects of Button, Text view and Edit text etc. classes.

Now look at the below code

Compiler will execute the above code when the user clicks on the erase button.

It will set the null or empty text on the edit text.

Now read the following code structure

When the user clicks “GET LATITUDE LONGITUDE” button, compiler will run the above code lines.

It will first get the address in the string format from the edit text.

Then it will create the object of the GeocodingLocation class.

Then compiler will user .getAddressFromLocation() method.

.getAddressFromLocation() method will get the address in string format as the first parameter, application context as a second parameter and object of GeocoderHandler() as the last parameter.

Below is the code writings for GeocoderHandler class.

Here, handleMessage() method will get the object of the Message class in it’s first parameter.

Then compiler will get the data from this object using Bundle class.

From this bundle, compiler will get the latitude and longitude in the string format using .getString(“address”) class.

Finally, compiler will set this latitude and longitude in the text view.

If you want to get address from latitude and longitude then read,

Android Get Location address from Latitude and Longitude

Android Get Current Address From Latitude and Longitude

Download Source Code for Android Get Latitude And Longitude From Address

Download Source Code for Address To LatLng

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 *