Google Map API with Kotlin

Here we are going to learn how to add a google map our project using google maps API with Kotlin.

Here we are developing a simple app with Google map and adding a marker to it.

Generate Google Maps API Key

Before we are starting to use a google map in our project we need to generate a Google Maps API key from Google API Console .

  • Login to Google API Console with your google account
  • Create a project then create credentials –>API Key as shown below
  • Copy Api Key Credential

Add Google Map into our Project

Now we need to add our API Key into our manifest.xml file under application tag.  Copy the below code and add it into the
manifest.xml under application tag.

manifest.xml

<meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="YOUR_API_KEY"
            />

Don’t forget to replace “YOUR_API_KEY” with your API key from Google API Console

Setup Layout

Now we need to setup our Layout for Google maps. Copy and paste the below code into activity_mail.xml file.

activity_mail.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <fragment xmlns:tools="http://schemas.android.com/tools"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </fragment>

</LinearLayout> 

Setup MainActivity.kt

To set up map in our application please copy and paste below code into your activity class.

package com.kotlincodes.googlemapwithkotlin

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions

class MainActivity : AppCompatActivity() ,OnMapReadyCallback {
    private var googleMap:GoogleMap?=null
   override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val mapFragment = supportFragmentManager
                .findFragmentById(R.id.map) as SupportMapFragment
        mapFragment.getMapAsync(this)

    }
    override fun onMapReady(p0: GoogleMap?) {
        googleMap=p0

        //Adding markers to map

        val latLng=LatLng(28.6139,77.2090)
        val markerOptions:MarkerOptions=MarkerOptions().position(latLng).title("New Delhi")

        // moving camera and zoom map

        val zoomLevel = 12.0f //This goes up to 21


        googleMap.let {
            it!!.addMarker(markerOptions)
            it.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoomLevel))
        }
    }
}

That’s it! Now run the project!

The full source code is available here.

Thanks for reading!