|
1 | 1 | --- |
2 | | -name: android-samples |
3 | | -description: Guide for understanding and using the Google Maps SDK for Android Samples repository. Use when users want to learn how to implement Maps SDK features, find examples, or create a new sample. |
| 2 | +name: maps-sdk-android |
| 3 | +description: Guide for integrating the Google Maps SDK for Android (Views/Fragments) into an Android application. Use when users ask to add Google Maps to their Android app without using Jetpack Compose. |
4 | 4 | --- |
5 | 5 |
|
6 | | -# Google Maps SDK for Android Samples |
| 6 | +# Google Maps SDK for Android Integration |
7 | 7 |
|
8 | | -You are an expert Android developer specializing in the Google Maps SDK for Android. This repository contains various samples demonstrating how to use the SDK. |
| 8 | +You are an expert Android developer specializing in the Google Maps SDK for Android using standard Android Views and Fragments. Follow these instructions carefully to integrate the Maps SDK into the user's application. |
9 | 9 |
|
10 | | -## Repository Structure |
| 10 | +## 1. Setup Dependencies |
11 | 11 |
|
12 | | -1. **ApiDemos**: A collection of small demos showing most features of the Maps SDK for Android (e.g., markers, polygons, camera movement, location). |
13 | | -2. **FireMarkers**: Demonstrates using Firebase Realtime Database with the Maps SDK. |
14 | | -3. **WearOS**: Demonstrates a basic map on a Wear OS device. |
15 | | -4. **tutorials**: Samples associated with tutorials in the developer's guide. |
16 | | -5. **snippets**: Snippets for code found in the official documentation. |
| 12 | +Add the necessary dependency to the app-level `build.gradle.kts` file: |
17 | 13 |
|
18 | | -## Running the Samples |
| 14 | +```kotlin |
| 15 | +dependencies { |
| 16 | + // Google Maps SDK for Android |
| 17 | + implementation("com.google.android.gms:play-services-maps:19.0.0") // Check for the latest version |
| 18 | +} |
| 19 | +``` |
19 | 20 |
|
20 | | -To run the samples, the user needs: |
21 | | -- A Google Maps API key added to `local.properties`: `MAPS_API_KEY=YOUR_API_KEY` |
22 | | -- The Secrets Gradle Plugin for Android is used to inject the API key. |
| 21 | +## 2. Setup the Secrets Gradle Plugin |
23 | 22 |
|
24 | | -## Creating a New Sample |
25 | | -When creating a new sample or modifying an existing one: |
26 | | -1. Ensure the code is written in Kotlin. |
27 | | -2. Follow modern Android development practices. |
28 | | -3. Use the Secrets Gradle plugin for API key management. |
29 | | -4. If adding a completely new feature demonstration, consider adding it to `ApiDemos`. |
| 23 | +Instead of hardcoding the Google Maps API key in `AndroidManifest.xml`, use the Secrets Gradle Plugin for Android to inject the API key securely. |
30 | 24 |
|
31 | | -## AI Guidelines |
32 | | -Always refer to `.geminiignore` to avoid indexing large media assets or build directories. |
| 25 | +First, add the plugin to the project-level `build.gradle.kts`: |
| 26 | + |
| 27 | +```kotlin |
| 28 | +buildscript { |
| 29 | + dependencies { |
| 30 | + classpath("com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1") |
| 31 | + } |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +Then, apply the plugin in the app-level `build.gradle.kts`: |
| 36 | + |
| 37 | +```kotlin |
| 38 | +plugins { |
| 39 | + // ... |
| 40 | + id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin") |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +Add the API Key to `local.properties`: |
| 45 | + |
| 46 | +```properties |
| 47 | +MAPS_API_KEY=YOUR_API_KEY |
| 48 | +``` |
| 49 | + |
| 50 | +In `AndroidManifest.xml`, reference the injected API key meta-data: |
| 51 | + |
| 52 | +```xml |
| 53 | +<manifest ...> |
| 54 | + <application ...> |
| 55 | + <!-- Google Maps API Key injected by Secrets Gradle Plugin --> |
| 56 | + <meta-data |
| 57 | + android:name="com.google.android.geo.API_KEY" |
| 58 | + android:value="${MAPS_API_KEY}" /> |
| 59 | + ... |
| 60 | + </application> |
| 61 | +</manifest> |
| 62 | +``` |
| 63 | + |
| 64 | +## 3. Implement the Map |
| 65 | + |
| 66 | +The standard way to implement a map is by using a `SupportMapFragment`. |
| 67 | + |
| 68 | +**activity_maps.xml:** |
| 69 | +```xml |
| 70 | +<?xml version="1.0" encoding="utf-8"?> |
| 71 | +<fragment xmlns:android="http://schemas.android.com/apk/res/android" |
| 72 | + xmlns:tools="http://schemas.android.com/tools" |
| 73 | + android:id="@+id/map" |
| 74 | + android:name="com.google.android.gms.maps.SupportMapFragment" |
| 75 | + android:layout_width="match_parent" |
| 76 | + android:layout_height="match_parent" |
| 77 | + tools:context=".MapsActivity" /> |
| 78 | +``` |
| 79 | + |
| 80 | +**MapsActivity.kt:** |
| 81 | +```kotlin |
| 82 | +import android.os.Bundle |
| 83 | +import androidx.appcompat.app.AppCompatActivity |
| 84 | +import com.google.android.gms.maps.CameraUpdateFactory |
| 85 | +import com.google.android.gms.maps.GoogleMap |
| 86 | +import com.google.android.gms.maps.OnMapReadyCallback |
| 87 | +import com.google.android.gms.maps.SupportMapFragment |
| 88 | +import com.google.android.gms.maps.model.LatLng |
| 89 | +import com.google.android.gms.maps.model.MarkerOptions |
| 90 | + |
| 91 | +class MapsActivity : AppCompatActivity(), OnMapReadyCallback { |
| 92 | + |
| 93 | + private lateinit var map: GoogleMap |
| 94 | + |
| 95 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 96 | + super.onCreate(savedInstanceState) |
| 97 | + setContentView(R.layout.activity_maps) |
| 98 | + |
| 99 | + // Obtain the SupportMapFragment and get notified when the map is ready to be used. |
| 100 | + val mapFragment = supportFragmentManager |
| 101 | + .findFragmentById(R.id.map) as SupportMapFragment |
| 102 | + mapFragment.getMapAsync(this) |
| 103 | + } |
| 104 | + |
| 105 | + override fun onMapReady(googleMap: GoogleMap) { |
| 106 | + map = googleMap |
| 107 | + |
| 108 | + // Add a marker in Singapore and move the camera |
| 109 | + val singapore = LatLng(1.35, 103.87) |
| 110 | + map.addMarker(MarkerOptions().position(singapore).title("Marker in Singapore")) |
| 111 | + map.moveCamera(CameraUpdateFactory.newLatLngZoom(singapore, 10f)) |
| 112 | + } |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +## 4. Best Practices & Guidelines |
| 117 | +* **Lifecycle Management:** `SupportMapFragment` is the recommended approach because it manages the map lifecycle automatically. If you must use a `MapView` directly in a layout, you **must** forward all Activity/Fragment lifecycle methods (`onCreate`, `onResume`, `onPause`, `onDestroy`, `onSaveInstanceState`, `onLowMemory`) to the `MapView`. |
| 118 | +* **Main Thread:** All interactions with the `GoogleMap` object must occur on the main UI thread. |
| 119 | +* **Permissions:** You do not need to request `ACCESS_FINE_LOCATION` or `ACCESS_COARSE_LOCATION` permissions unless you are explicitly enabling the "My Location" layer via `map.isMyLocationEnabled = true`. |
0 commit comments