Android Tech Help – How to Geocode and Reverse Geocode in Android for creating Location Based Apps

08 Jul. 2015 Mobile App

A few years ago, GPS technology was very expensive and difficult to understand. This was a major hurdle for mobile developers who wanted to create location based apps. Today, almost every smartphone has a GPS receiver and is truly becoming ubiquitous in all devices. This blog is a part of Android Tech Help series by Rishabh Software and explains how you can leverage Google Maps and OpenStreetmap for geocoding and reverse geocoding on Android devices.

Today many organizations leverage OpenStreetMap (openstreetmap.org) for developing location based apps. OpenStreetMap is a free, editable map of the world created by volunteers using local knowledge technology. On the other hand, Google gets most of the GPS data by using commercial mapping services.

geocoding and reverse geocoding addresses on android

Leveraging Google Map & OpenStreetMap for Android Applications

Problem

How to Geocode and Reverse Geocode Map on Android

Solution

We can achieve this by using Geocoder class.

Geocoding is the process of finding the geographical coordinates (latitude and longitude) of a given address or location. Reverse Geocoding is the opposite of geocoding where a pair of latitude and longitude is converted into an address or location.

For achieving Geocode or Reverse Geocode you must first import the proper package.

import android.location.Geocoder;

The geocoding or reverse geocoding operation needs to be done on a separate thread and should never be used on the UI thread as it will cause the system to display an Application Not Responding (ANR) dialog to the user.

To Achieve Geocode, use the below code

Geocoder gc = new Geocoder(context);
if(gc.isPresent()){
List<Address> list = gc.getFromLocationName(“155 Park Theater, Palo Alto, CA”, 1);
Address address = list.get(0);
double lat = address.getLatitude();
double lng = address.getLongitude();
}

To Achieve Reverse Geocode, use the below code

Geocoder gc = new Geocoder(context);
if(gc.isPresent()){
List<address> list = gc.getFromLocation(37.42279, -122.08506,1);
Address address = list.get(0);
StringBuffer str = new StringBuffer();
str.append(“Name: ” + address.getLocality() + “\n”);
str.append(“Sub-Admin Ares: ” + address.getSubAdminArea() + “\n”);
str.append(“Admin Area: ” + address.getAdminArea() + “\n”);
str.append(“Country: ” + address.getCountryName() + “\n”);
str.append(“Country Code: ” + address.getCountryCode() + “\n”);
String strAddress = str.toString();
}

Are you currently facing challenges in developing location based mobile applications? And would want a mobile team with experience to help you out?

Get a Free ConsultationTalk to our team to help you develop quality location based Android apps today.

 

Have a look at our location based solutions created for enterprise clients:

Follow Us
Subscribe to the Blog
Get a Free Consultation
Reach out to our team to get a free consultation for your next projectContact Us