Google Maps is a web-based mapping service that can be integrated into an Android application.
The Google Maps Android API is a part of the Google Play Services. So, we need to add play-services-maps
dependency in the module's build.gradle
file.
dependencies {
// Other dependencies
// ...
implementation 'com.google.android.gms:play-services-maps:17.0.0'
}
To access the Google Maps servers, we need an API key. It is a unique identifier that is used for requests authentication. API key is free, and we can get it by following these steps:
- Login to the Google Developers Console.
- Click the drop-down and select the project or create new.
- In the navigation bar, click "Credentials".
- When credentials page was opened, click "Create credentials" and then select "API key".
- The newly created API key will be provided in the dialog.
- Copy API key and add to
strings.xml
file.
<resources>
<!-- Other strings -->
<string name="google_api_key" templateMergeStrategy="preserve"
translatable="false">YOUR_API_KEY</string>
</resources>
- Open the
AndroidManifest.xml
file and add themeta-data
element which defines an API key.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">
<application>
<!-- ... -->
<meta-data android:name="com.google.android.geo.API_KEY"
android:value="@string/google_api_key"/>
<activity android:name=".MainActivity">
<!-- ... -->
</activity>
</application>
</manifest>
In the layout XML file we added <fragment>
element which will used to display Google map. A SupportMapFragment
provides access to the GoogleMap
object.
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/myMap"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" />
The findFragmentById()
method can be used to find a map fragment that was identified by ID. Map callback can be registered by using getMapAsync()
method.
After that, we implement the OnMapReadyCallback
interface and override the onMapReady()
method. We move the camera to the desired location by using the moveCamera()
method.
package com.example.app
import androidx.appcompat.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
class MainActivity : AppCompatActivity(), OnMapReadyCallback
{
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val mapFragment = supportFragmentManager
.findFragmentById(R.id.myMap) as SupportMapFragment
mapFragment.getMapAsync(this)
}
override fun onMapReady(googleMap: GoogleMap)
{
val cords = LatLng(40.689247, -74.044502)
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(cords, 14f))
}
}
Leave a Comment
Cancel reply