Probashi Link - প্রবাসীদের সকল সমস্যা এক অ্যাপেই সমাধানDownload Now!

Author Profile

My photo
Bangladesh

Search This Blog

How to Get City, Country Name, Latitude & Longitude in Android (API 33+ Compatible)

Admin
0

How to Get Country Name, City, Latitude & Longitude in Android (Modern API 33+ Compatible)

Have you ever wondered how apps like weather trackers, food delivery services, or Islamic prayer time apps automatically detect your exact city and country? It all relies on two core features: fetching GPS coordinates and converting them into a human-readable address.

In this guide, you will learn step-by-step how to get your user's exact Latitude, Longitude, City, and Country Name using Google’s FusedLocationProviderClient and Geocoder without crashing your app or running into deprecation warnings on newer Android versions.


How to Get Country Name, City, Latitude & Longitude in Android (Modern API 33+ Compatible)
How to Get City, Country Name, Latitude & Longitude in Android (API 33+ Compatible)


1. Understanding the Architecture

To build a seamless location module in Android, we break the workflow into two major steps:

  • Fetching Raw Coordinates: Using Google Play Services to retrieve high-accuracy Latitude and Longitude values.
  • Reverse Geocoding: Passing those coordinates into Android's Geocoder to parse address parameters like city name and country name.

💡 Pro Tip:

Always request runtime permissions in your main Activity before triggering any location service to avoid runtime permission exceptions!

2. Granting Required Location Permissions

Before executing any location fetching logic, your Android app must request and obtain location permissions from the user. Without these permissions, calling location services will throw a SecurityException and crash your app.

A. Declare Permissions in AndroidManifest.xml

Add both Fine and Coarse location permissions inside your AndroidManifest.xml file:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

B. Request Runtime Permission in Activity

Starting from Android 6.0 (API level 23), declaring permissions in the manifest isn't enough. You must prompt the user at runtime before starting the location service:

Step 1: Check if permission is granted using ContextCompat.checkSelfPermission().
Step 2: If not granted, trigger ActivityCompat.requestPermissions() to display the system permission dialog.
Step 3: Once granted, initialize your LocationHelper to fetch coordinates.

3. Understanding Address Parameters

When Geocoder returns address results, it provides multiple fields. Knowing which field to fallback on ensures your app never displays "Unknown" or empty strings to your users:

Parameter What It Represents Example
Locality Primary City or Town name Dhaka, New York
SubAdminArea District or Sub-region (Fallback if City is null) Gazipur, Brooklyn
CountryName Full Country name Bangladesh, United States

4. Handling Android 13 (API 33+) Deprecation

Prior to Android 13, geocoder.getFromLocation() ran synchronously, which could block the main thread and cause Application Not Responding (ANR) dialogs if network calls hung.

Google introduced an asynchronous callback-based GeocodeListener in API 33. A production-ready app must check the device SDK version at runtime and branch the logic accordingly to support older devices alongside modern Android phones seamlessly.


🚀 Get the Full Source Code

The complete implementation—including permission checks, location client configuration, and Android 13 backwards-compatible Geocoder code—is available on GitHub.

📦 View Source Code on Gist.GitHub

Post a Comment

0 Comments

*Don't spam! All comments reviewed by Admin

Post a Comment

Mohammad Harun

Mohammad Harun is a professional Android App developer and blog writer Know more..
To Top