Geolocation and Maps in Kotlin: A Complete Guide to Integration

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Geolocation and Maps in Kotlin: A Complete Guide for Beginners

Modern mobile applications increasingly use user location data. Kotlin, being the primary language for Android development, provides powerful tools for working with geolocation and displaying maps. In this article, we will thoroughly examine how to integrate maps and obtain coordinates in your Kotlin application.



1. Obtaining Geolocation in Kotlin

To work with geolocation in Android, the LocationManager class or the more modern FusedLocationProviderClient from Google Play Services is used. Let's look at both approaches.



1.1. Using LocationManager (Basic Method)

This method is suitable for simple tasks and does not require connecting Google Play Services.

import android.location.Locationimport android.location.LocationListenerimport android.location.LocationManagerimport android.os.Bundle

class LocationHelper(private val locationManager: LocationManager) {

fun getCurrentLocation() { val locationListener = object : LocationListener { override fun onLocationChanged(location: Location) { val lat = location.latitude val lon = location.longitude println("Latitude: $lat, Longitude: $lon") }

override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {} override fun onProviderEnabled(provider: String) {} override fun onProviderDisabled(provider: String) {} }

// Request location updates locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 5000L, // Interval in milliseconds 10f, // Minimum distance change in meters locationListener ) }}


1.2. Using FusedLocationProviderClient (Recommended Method)

This API is more accurate and energy-efficient as it uses a combination of GPS, Wi-Fi, and cellular towers.

import com.google.android.gms.location.FusedLocationProviderClientimport com.google.android.gms.location.LocationServicesimport com.google.android.gms.tasks.Task

class FusedLocationHelper(private val fusedLocationClient: FusedLocationProviderClient) {

fun getLastLocation() { val task: Task<Location> = fusedLocationClient.lastLocation task.addOnSuccessListener { location -> if (location != null) { val lat = location.latitude val lon = location.longitude println("Last location: $lat, $lon") } else { println("Location not found") } } }}


2. Integrating Maps in Kotlin

To display maps, Google Maps SDK or OpenStreetMap are most commonly used. Let's start with the most popular option.



2.1. Google Maps SDK

To work with Google Maps, you need to obtain an API key and add the dependency in build.gradle:

// build.gradle (Module)dependencies {    implementation 'com.google.android.gms:play-services-maps:18.2.0'}


Example of adding a map to an Activity:

import android.os.Bundleimport androidx.appcompat.app.AppCompatActivityimport com.google.android.gms.maps.CameraUpdateFactoryimport com.google.android.gms.maps.GoogleMapimport com.google.android.gms.maps.OnMapReadyCallbackimport com.google.android.gms.maps.SupportMapFragmentimport com.google.android.gms.maps.model.LatLngimport com.google.android.gms.maps.model.MarkerOptions

class MapsActivity : AppCompatActivity(), OnMapReadyCallback {

private lateinit var mMap: GoogleMap

override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentVie

Recommendations