Skip to content

Google Maps android example with Kotlin – Easy

Google Maps Android: With maps SDK you can use google maps in android. There is much other application in the market using google maps like UBER, OLA, HOUSING, etc. Google Android Maps API v2 handles access to Google Maps server, map display, data downloading, and response to me map gestures.

You can also change the user’s view of a particular map area, add markers, polygons, overlays, and direction etc.

The Maps API allows you to add these graphics to a map:

1. Markers: Icons anchored to specific positions on the map.google-maps-marker-icon Very useful to show marks like Restaurants, your places, service-providing centers, etc.

2. Polylines: Sets of line segments. Examples using google Maps and click for directions, second cab booking in UBER,  then you can see Polylines.

Polylines google maps android

3. Polygons: Enclosed segments. You can use for show user coverage area of your service like delivery area 5km.

3. Bitmap graphics anchored to specific positions on the map (Ground Overlays).

4. Sets of images which are displayed on top of the base map tiles (Tile Overlays).

Let’s Build Google Maps Android Example :

Many more things you can do with Google Android Maps API v2, in this application you will learn the basic implementation of Google maps SDK API in Android. It’s very easy with new Google instruction to let’s see.

Step 1: Download Android Studio And install it.
Step 2: Create a project new
Google Maps Android Kotlin example tutorials
  • Otherwise, click File in the Android Studio menu bar, then NewNew Project.
Google Maps Android Kotlin example tutorials

Step 2.1 Enter your application name, company domain, and project location, as prompted. Then click Next.

Google Maps Android Kotlin

Step 2.2 Select the form factors you need for your app. If you’re not sure what you need, just select Phone and Tablet. Then click Next.

Google Maps Android Kotlin example tutorial

Step 2.3 Select Google Maps Activity in the ‘Add an activity to Mobile’ dialog. Then click Next. Its Important step you can add a map in the app later. In another tutorial, we will cover another way.

Google Maps Android Kotlin example tutorial

Step 2.4. Enter the activity name, layout name, and title as prompted. The default values are fine. Then click Finish.

Google Maps Android Kotlin example tutorial

Half work has done now need to get API key from google console.

Step 3.  Now Get a Google Maps API key

Now you need an API key to access Google Maps servers. Google is providing many ways to get API keys. Choose one of the following ways :

  1. The fast, easy way [Preferred]: Use the link provided in the google_maps_api.xml (res/values/google_maps_api.xml) file that Android Studio created for you:
    1. Copy the link provided in the google_maps_api.xml file and paste it into your browser. The link takes you to the Google Cloud Platform Console and supplies the required information to the Google Cloud Platform Console via URL parameters.Google Maps Android Kotlin example tutorials
    2. Follow the instructions to create a new project on the Google Cloud Platform Console or select an existing project.
    3. Create an Android-restricted API key for your project.
    4. Copy the resulting API key, go back to Android Studio, and paste the API key into the <string> element in thegoogle_maps_api.xml (res/values/google_maps_api.xml)file.
  • Detailed guides to getting an API key:
    1. Go to the Google Cloud Platform Console.
    2. Create or select a project.
    3. Click Continue to enable the Maps SDK for Android.
    4. On the Credentials page, get an API key.
      Note: If you have an existing API key with Android restrictions, you may use that key.
    5. From the dialog displaying the API key, select Restrict key to set an Android restriction on the API key.
    6. In the Restrictions section, select Android apps, then enter your app’s SHA-1 fingerprint and package name. For example
      BB:0D:AC:74:D3:21:E1:43:67:71:9B:62:91:AF:A1:66:6E:44:5D:75
      com.example.android.mapexample

      Click Save.

Step 4. Complete code

4.1 Activity_maps.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="in.eyehunt.googlemapsandroidkotlin.MapsActivity" />

4.2 MapsActivity.kt

package `in`.eyehunt.googlemapsandroidkotlin

import android.support.v7.app.AppCompatActivity
import android.os.Bundle

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 MapsActivity : AppCompatActivity(), OnMapReadyCallback {

    private lateinit var mMap: GoogleMap

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_maps)
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        val mapFragment = supportFragmentManager
                .findFragmentById(R.id.map) as SupportMapFragment
        mapFragment.getMapAsync(this)
    }

    override fun onMapReady(googleMap: GoogleMap) {
        mMap = googleMap

        // Add a marker in Sydney and move the camera
        val sydney = LatLng(-34.0, 151.0)
        mMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney"))
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney))
    }
}

 

Step 5. Now Run the application, in an emulator or On your Android device

Output screenshot Google Maps android :

Google Maps API android exmaple tutorial

Download Link Google Maps android in Github source code  :

https://github.com/EyeHunts/GoogleMapsAndroidKotlin

Note: This example (Project) is developed in Android Studio 3.0.1, tested on Android 9 ( Android P), compile SDK version API 26: Android 8.0 (Oreo)

MinSdkVersion=”15″

TargetSdkVersion=”26″

Coding in Kotlin

1 thought on “Google Maps android example with Kotlin – Easy”

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading