How to use Google Maps SDK in an Android app?

To use the Google Maps SDK in your Android application, follow these steps to integrate the features you need for displaying maps, markers, and more.

Step 1: Set Up Your Project

First, you need to create a new project in Android Studio and add the Google Maps dependency. Open your app-level build.gradle file and add the following line in the dependencies block:

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

Step 2: Get an API Key

Next, you must obtain an API key from the Google Cloud Console. Make sure to enable the "Maps SDK for Android" API for your project.

Step 3: Add Permissions

Update your AndroidManifest.xml file to include the necessary permissions and your API key:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.yourapp"> <application ... > <meta-data android:name="com.google.android.geo.API_KEY" android:value="YOUR_API_KEY"/> <activity android:name=".MapsActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> </manifest>

Step 4: Create a Maps Activity

Create a new activity named MapsActivity. Here is a basic implementation to display the map:

import androidx.fragment.app.FragmentActivity; 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; public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { private GoogleMap mMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; // Add a marker in Sydney and move the camera LatLng sydney = new LatLng(-34, 151); mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); } }

Step 5: Layout Setup

In your activity_maps.xml, add the map fragment:

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

Google Maps SDK Android Development Maps Integration Google Maps API Key Mobile Mapping