Introduction to Geolocation and Maps in Kotlin
Kotlin is a modern programming language widely used for Android application development. Working with geolocation and maps is one of the key capabilities of mobile applications. In this article, we will review the main libraries and tools for integrating maps, obtaining user coordinates, and building routes in Kotlin.
1. Google Maps SDK for Android
Google Maps is the most popular service for displaying maps. To use it in Kotlin, you need to add the dependency in build.gradle:
dependencies { implementation 'com.google.android.gms:play-services-maps:18.2.0' implementation 'com.google.android.gms:play-services-location:21.0.1'}After adding the API key to the manifest, you can display a map in a fragment:
import com.google.android.gms.maps.*import com.google.android.gms.maps.model.*
class MapsFragment : SupportMapFragment(), OnMapReadyCallback { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) getMapAsync(this) }
override fun onMapReady(googleMap: GoogleMap) { val sydney = LatLng(-33.852, 151.211) googleMap.addMarker(MarkerOptions().position(sydney).title("Sydney")) googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 15f)) }}2. Getting Current Location
The Fused Location Provider is used for working with geolocation. Example of getting the last known location:
import com.google.android.gms.location.*
class LocationHelper(private val context: Context) { private val fusedLocationClient = LocationServices.getFusedLocationProviderClient(context)
fun getLastLocation() { fusedLocationClient.lastLocation.addOnSuccessListener { location: Location? -> location?.let { val lat = it.latitude val lng = it.longitude Log.d("Location", "Latitude: $lat, Longitude: $lng") } } }}For continuous tracking, use LocationRequest and LocationCallback:
val locationRequest = LocationRequest.Builder( Priority.PRIORITY_HIGH_ACCURACY, 10000L).build()
val locationCallback = object : LocationCallback() { override fun onLocationResult(locationResult: LocationResult) { for (location in locationResult.locations) { // Process new coordinates } }}
fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper())3. OpenStreetMap and the osmdroid Library
If you need a free alternative to Google Maps, use osmdroid. Setup:
dependencies { implementation 'org.osmdroid:osmdroid-android:6.1.18'}Example of displaying a map and a marker:
import org.osmdroid.config.Configurationimport org.osmdroid.tileprovider.tilesource.TileSourceFactoryimport org.osmdroid.views.MapViewimport org.osmdroid.util.GeoPointimport org.osmdroid.views.overlay.Marker
class OsmMapActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) Configuration.getInstance().userAgentValue = packageName setContentView(R.layout.activity_osm)
val mapView = findViewById(R.id.map) mapView.setTileSource(TileSourceFactory.MAPNIK) mapView.controller.setZoom(15.0) val startPoint = GeoPoint(55.7558, 37.6173) // Moscow mapView.controller.setCenter(startPoint)
val marker = Marker(mapView) marker.position = startPoint marker.title = "Moscow" marker.snippet = "Red Also in library
JSON Processing in JavaScript (Node.js): A Complete Guide
Authorization and Authentication in Swift: A Complete Guide for iOS/macOS
C++ Standard Library: Complete Overview and Code Examples
Dependency Injection Container for C# (.NET) — Complete Guide
Bucket4j Java Guide: Rate Limiting with Examples