EXP9

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Ex NO:9 Develop an Application by Integrating Google Maps

Developing a location-based app in Kotlin using Google Maps API


In this tutorial, we will learn how to create a location-based app using Kotlin and Google Maps API.
Location-based apps are essential in various fields like navigation, travel, and social media. To follow
this tutorial, you should have basic knowledge of Kotlin programming and Android Studio.

Prerequisites
 Android Studio installed on your computer.

 Basic knowledge of Kotlin programming language.

 A Google Cloud Platform account to access Google Maps API.

Step 1: Setup a New Android Studio Project


Open Android Studio and create a new project. Select "Empty Activity" as your project template and
click "Next". Name your project, choose Kotlin as the programming language, and set the minimum
SDK version to Android 4.1 (API 16) or higher. Click "Finish" to create your project.

Step 2: Add Google Maps API Key


Visit the Google Cloud Platform and enable the "Maps SDK for Android" API. Create a new API key
and restrict it to Android apps. Copy the generated API key.

In your Android Studio project, open the "AndroidManifest.xml" file and add the following code
inside the <application> tag:

<meta-data

android:name="com.google.android.geo.API_KEY"

android:value="YOUR_API_KEY"/>

XML

Copy

Replace "YOUR_API_KEY" with the API key you generated earlier.

Step 3: Add Google Maps Dependencies


Open the "build.gradle (Module: app)" file and add the following dependencies:

dependencies {

implementation 'com.google.android.gms:play-services-maps:17.0.1'

Gradle

Copy

Click "Sync Now" to sync the project with the new dependencies.

Step 4: Create a Custom Map Fragment


Create a new Kotlin class named "CustomMapFragment" that extends "SupportMapFragment".
Override the "onActivityCreated()" method and implement the "OnMapReadyCallback" interface.
Inside the "onActivityCreated()" method, get the GoogleMap object and set the map type:

import androidx.appcompat.app.AppCompatActivity

import android.os.Bundle

import androidx.fragment.app.FragmentActivity

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 CustomMapFragment : 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))

Java

Copy

Step 5: Update the Layout File


Open the "activity_maps.xml" layout file and add the "CustomMapFragment" class as the fragment:

<fragment

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/map"

android:name="com.example.locationbasedapp.CustomMapFragment"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".CustomMapFragment" />

XML

Copy

Step 6: Test Your App


Build and run your app on an Android device or emulator. You should see a map with a marker in
Sydney.

Congratulations! You have successfully created a location-based app using Kotlin and Google Maps
API. You can now customize the app further by adding more features like user location, search
functionality, and custom markers.

You might also like