Android – MapView tutorial – Display current location on map in Android

Android - MapView tutorial - Display current location on map in Android

Add MapView in xml

        app:layout_constraintTop_toTopOf="parent" />

Get google map key

  1. Create project in google cloud
  2. Enable Android SDK for google maps under Other solution -> Google Maps -> API
  3. Get your API key in API & Services -> Credential
  4. If API key is not already there create new API key
  5. Copy API Key
  6. Add in Manifest.xml file under application tag : <meta-data android:name=”” android:value=”YOUR_API_KEY”/>

Load map


        //Implement OnMapReadyCallback
        //this will override 

        override fun onMapReady(map: GoogleMap?) {
             //You can store map and use later

Get Last known location or Get current location

Now we need to get our current location. Here is simple code which will give us device’s last known location. Below code will get last location.

       if (permissionGranted(Manifest.permission.ACCESS_FINE_LOCATION)) {
                .lastLocation.addOnSuccessListener { location ->
                if(location != null)

Add marker on map

This is simple way to addMarker at particular position in android

   private fun displayCurrentLocationOnMap(location: Location) {
        if (googleMap == null) return //googleMap is previously stored from onMapReady
        val latLng = LatLng(location.latitude, location.longitude)
        val cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 12.6f)
//        googleMap?.addMarker(MarkerOptions().position(latLng).draggable(true))

Share this content:


More Posts

Introduction to Kotlin: A Versatile and Modern Programming Language

Kotlin, a versatile and modern programming language, offers developers a concise, safe, and interoperable coding experience. With features like null safety, extension functions, and coroutines, Kotlin enhances productivity and readability, making it an attractive choice for Android development and beyond. Whether you’re new to programming or an experienced developer, exploring Kotlin opens up a world of possibilities for building robust and expressive applications.

Mastering the Android Activity Lifecycle: A Comprehensive Guide

The Android Activity Lifecycle is a fundamental concept for Android developers. It defines how an Activity behaves during its lifecycle, from creation to destruction. Understanding the Activity Lifecycle is crucial for managing resources efficiently and delivering a smooth user experience. In this blog post, we’ll briefly introduce the different states an Activity can be in and the main callback methods associated with each state. Let’s dive in and explore this important aspect of Android app development!

Table of Contents

Send Us A Message