Maps SDK for Android Quickstart  |  Google for Developers (2024)

New basemap styling is coming soon to Google Maps Platform. This update to map styling includes a new default color palette, modernized pins, and improvements to map experiences and usability. All map styles will be automatically updated in March 2025. For more information on availability and how to opt in earlier, see New map style for Google Maps Platform.

Stay organized with collections Save and categorize content based on your preferences.

Create an Android app that displays a map by using the Google Maps Viewstemplate for Android Studio. If you have an existing Android Studio project thatyou'd like to set up, see Set up an Android Studio project.

This quickstart is intended for developers who are familiar with basicAndroid development with Kotlin or Java.

About the development environment

This quickstart was developed using Android StudioHedgehog and the AndroidGradle pluginversion 8.2.

Set up an Android device

To run an app that uses the Maps SDK for Android, you must deploy it to an Android device or Android emulator that is based on Android 5.0 or higher and includes the Google APIs.

Create a Google Maps project in Android Studio

The procedure to create a Google Maps project in Android Studio was changed inthe Flamingo and later releases of Android Studio.

  1. Open Android Studio, and click New Project in theWelcome to Android Studio window.

  2. In the New Project window, under the Phone and Tablet category,select No Activity, and then click Next.

  3. Complete the New Project form:

    • Set Language to Java or Kotlin. Both languages are fully supported bythe Maps SDK for Android. To learn more about Kotlin, seeDevelop Android apps with Kotlin.

    • Set Minimum SDK to an SDK version compatible with your test device.You must select a version greater than the minimum version required by theMaps SDK for Android version 19.0.x,which is Android API Level 21 ("Lollipop"; Android 5.0) or higher.See the Release Notes forthe latest information on the SDK version requirements.

    • Set the Build configuration language to Kotlin DSL or Groovy DSL.Snippets for both build configurations languages are shown in the followingprocedures.

  4. Click Finish.

    Android Studio starts Gradle and builds the project. This may take some time.

  5. Add the Google Maps Views Activity:

    1. Right-click on the app folder in your project.
    2. Select New > Google > Google Maps Views Activity.

      Maps SDK for Android Quickstart | Google for Developers (3)

    3. In the New Android Activity dialog box, select theLauncher Activity checkbox.

    4. Select Finish.

      For more information, seeAdd code from a template

  6. When the build is finished, Android Studio opens the AndroidManifest.xmland MapsActivity files. Your activity may have a different name, but itis the one you configured during setup.

Set up your Google Cloud project

Complete the required Cloud Console setup steps by clickingthrough the following tabs:

Step 1

Console

  1. In the Google Cloud Console, on the project selector page, click Create Project to begin creating a new Cloud project.

    Go to the project selector page

  2. Make sure that billing is enabled for your Cloud project. Confirm that billing is enabled for your project.

    Google Cloud offers a $0.00 charge trial. The trial expires at either end of 90 days or after the account has accrued $300 worth of charges, whichever comes first. Cancel anytime. Google Maps Platform features a recurring $200 monthly credit. For more information, see Billing account credits and Billing.

Cloud SDK

gcloud projects create "PROJECT"

Read more about the Google Cloud SDK , Cloud SDK installation , and the following commands:

Step 2

To use Google Maps Platform, you must enable the APIs or SDKs you plan to use with your project.

Console

Enable the Maps SDK for Android

Cloud SDK

gcloud services enable \ --project "PROJECT" \ "maps-android-backend.googleapis.com"

Read more about the Google Cloud SDK , Cloud SDK installation , and the following commands:

Step 3

This step only goes through the API Key creation process. If you use your API Key in production, we strongly recommend that you restrict your API key. You can find more information in the product-specific Using API Keys page.

The API key is a unique identifier that authenticates requests associated with your project for usage and billing purposes. You must have at least one API key associated with your project.

To create an API key:

Console

  1. Go to the Google Maps Platform > Credentials page.

    Go to the Credentials page

  2. On the Credentials page, click Create credentials > API key.
    The API key created dialog displays your newly created API key.
  3. Click Close.
    The new API key is listed on the Credentials page under API keys.
    (Remember to restrict the API key before using it in production.)

Cloud SDK

gcloud alpha services api-keys create \ --project "PROJECT" \ --display-name "DISPLAY_NAME"

Read more about the Google Cloud SDK , Cloud SDK installation , and the following commands:

Add the API key to your app

This section describes how to store your API key so that it can be securely referenced byyour app. You should not check your API key into your version control system, so we recommendstoring it in the secrets.properties file, which is located in the root directory of yourproject. For more information about the secrets.properties file, seeGradle properties files.

To streamline this task, we recommend that you use theSecrets Gradle Plugin for Android.

To install the Secrets Gradle Plugin for Android in your Google Maps project:

  1. In Android Studio, open your top-level build.gradle.kts or build.gradle file and add the following code to the dependencies element under buildscript.

    Kotlin

    buildscript { dependencies { classpath("com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1") }}

    Groovy

    buildscript { dependencies { classpath "com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1" }}
  2. Open your module-level build.gradle.kts or build.gradle file and add the following code to the plugins element.

    Kotlin

    plugins { // ... id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin")}

    Groovy

    plugins { // ... id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'}
  3. In your module-level build.gradle.kts or build.gradle file, ensure that targetSdk and compileSdk are set to 34.
  4. Save the file and sync your project with Gradle.
  5. Open the secrets.properties file in your top-level directory, and then add the following code. Replace YOUR_API_KEY with your API key. Store your key in this file because secrets.properties is excluded from being checked into a version control system.
    MAPS_API_KEY=YOUR_API_KEY
  6. Save the file.
  7. Create the local.defaults.properties file in your top-level directory, the same folder as the secrets.properties file, and then add the following code.

    MAPS_API_KEY=DEFAULT_API_KEY

    The purpose of this file is to provide a backup location for the API key if the secrets.properties file is not found so that builds don't fail. This can happen if you clone the app from a version control system which omits secrets.properties and you have not yet created a secrets.properties file locally to provide your API key.

  8. Save the file.
  9. In your AndroidManifest.xml file, go to com.google.android.geo.API_KEY and update the android:value attribute. If the <meta-data> tag does not exist, create it as a child of the <application> tag.
    <meta-data android:name="com.google.android.geo.API_KEY" android:value="${MAPS_API_KEY}" />

    Note: com.google.android.geo.API_KEY is the recommended metadata name for the API key. A key with this name can be used to authenticate to multiple Google Maps-based APIs on the Android platform, including the Maps SDK for Android. For backwards compatibility, the API also supports the name com.google.android.maps.v2.API_KEY. This legacy name allows authentication to the Android Maps API v2 only. An application can specify only one of the API key metadata names. If both are specified, the API throws an exception.

  10. In Android Studio, open your module-level build.gradle.kts or build.gradle file and edit the secrets property. If the secrets property does not exist, add it.

    Edit the properties of the plugin to set propertiesFileName to secrets.properties, set defaultPropertiesFileName to local.defaults.properties, and set any other properties.

    Kotlin

    secrets { // To add your Maps API key to this project: // 1. If the secrets.properties file does not exist, create it in the same folder as the local.properties file. // 2. Add this line, where YOUR_API_KEY is your API key: // MAPS_API_KEY=YOUR_API_KEY propertiesFileName = "secrets.properties" // A properties file containing default secret values. This file can be // checked in version control. defaultPropertiesFileName = "local.defaults.properties" // Configure which keys should be ignored by the plugin by providing regular expressions. // "sdk.dir" is ignored by default. ignoreList.add("keyToIgnore") // Ignore the key "keyToIgnore" ignoreList.add("sdk.*") // Ignore all keys matching the regexp "sdk.*"} 

    Groovy

    secrets { // To add your Maps API key to this project: // 1. If the secrets.properties file does not exist, create it in the same folder as the local.properties file. // 2. Add this line, where YOUR_API_KEY is your API key: // MAPS_API_KEY=YOUR_API_KEY propertiesFileName = "secrets.properties" // A properties file containing default secret values. This file can be // checked in version control. defaultPropertiesFileName = "local.defaults.properties" // Configure which keys should be ignored by the plugin by providing regular expressions. // "sdk.dir" is ignored by default. ignoreList.add("keyToIgnore") // Ignore the key "keyToIgnore" ignoreList.add("sdk.*") // Ignore all keys matching the regexp "sdk.*"} 

Look at the code

Examine the code supplied by the template. In particular, look at the followingfiles in your Android Studio project.

Maps activity file

The maps activity file is the main activity for the app, andcontains the code to manage and display the map. By default, the file thatdefines the activity is named MapsActivity.java or if you set Kotlin as thelanguage for your app, MapsActivity.kt.

The main elements of the maps activity:

  • The SupportMapFragment object manages thelifecycle of the map and is the parent element of the app's UI.

  • The GoogleMap object provides access to the map data andview. This is the main class of the Maps SDK for Android. The Map Objectsguide describes the SupportMapFragment and GoogleMap objects in moredetail.

  • The moveCamera function centers the map at theLatLng coordinates for Sydney Australia. The first settings toconfigure when adding a map are usually the map location and camera settings;such as viewing angle, map orientation, and zoom level. See theCamera and View guide for details.

  • The addMarker function adds a marker to the coordinates forSydney. See the Markers guide for details.

Module Gradle file

The Module build.gradle.kts file includes the following maps dependency, whichis required by the Maps SDK for Android.

dependencies { // Maps SDK for Android implementation("com.google.android.gms:play-services-maps:19.0.0")}

To learn more about managing the Maps dependency, see Versioning.

XML layout file

The activity_maps.xml file is the XML layout file that defines thestructure of the app's UI. The file is located in the res/layout directory.The activity_maps.xml file declares a fragment that includes the followingelements:

  • tools:context sets the default activity of the fragmentto MapsActivity, which is defined in the maps activity file.
  • android:name sets the class name of the fragment toSupportMapFragment, which is the fragment type used in the maps activityfile.

The XML layout file contains the following code:

<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=".MapsActivity" />

Deploy and run the app

Maps SDK for Android Quickstart | Google for Developers (4)

When you run the app successfully, it will display a map that is centered onSydney Australia with a marker on the city as seen in the following screenshot.

To deploy and run the app:

  1. In Android Studio, click the Run menu option (or the play button icon) to run your app.
  2. When prompted to choose a device, choose one of the following options:
    • Select the Android device that's connected to your computer.
    • Alternatively, select the Launch emulator radio button and choose the virtual device that you set up.
  3. Click OK. Android Studio will start Gradle to build your app, and then display the results on your device or emulator. It can take several minutes before the app launches.

Next steps

  • Set up a map: This documentdescribes how to set up the initial and runtime settings for your map, suchas the camera position, map type, UI components, and gestures.

  • Add a map to your Android app (Kotlin): This codelabwalks you through an app that demonstrates some additional features of theMaps SDK for Android.

  • Use the Maps Android KTX library:This Kotlin extensions (KTX) library lets you take advantage of severalKotlin language features while using the Maps SDK for Android.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2024-09-16 UTC.

Maps SDK for Android Quickstart  |  Google for Developers (2024)
Top Articles
Sealed Trailer - Copper Run
Error
Bj 사슴이 분수
How To Do A Springboard Attack In Wwe 2K22
Sarah F. Tebbens | people.wright.edu
Txtvrfy Sheridan Wy
How to Type German letters ä, ö, ü and the ß on your Keyboard
What Happened To Father Anthony Mary Ewtn
Carter Joseph Hopf
Anki Fsrs
Nexus Crossword Puzzle Solver
Mephisto Summoners War
ocala cars & trucks - by owner - craigslist
Dallas Cowboys On Sirius Xm Radio
Sonic Fan Games Hq
Www Craigslist Milwaukee Wi
Metro Pcs.near Me
Craigslist Clinton Ar
Morristown Daily Record Obituary
Wisconsin Volleyball Team Boobs Uncensored
Project Reeducation Gamcore
Hannaford Weekly Flyer Manchester Nh
Move Relearner Infinite Fusion
Table To Formula Calculator
Mynahealthcare Login
Lbrands Login Aces
How rich were the McCallisters in 'Home Alone'? Family's income unveiled
24 Hour Drive Thru Car Wash Near Me
Does Royal Honey Work For Erectile Dysfunction - SCOBES-AR
Craig Woolard Net Worth
Prévisions météo Paris à 15 jours - 1er site météo pour l'île-de-France
ShadowCat - Forestry Mulching, Land Clearing, Bush Hog, Brush, Bobcat - farm & garden services - craigslist
Bee And Willow Bar Cart
Watchdocumentaries Gun Mayhem 2
Everything You Need to Know About NLE Choppa
Ny Post Front Page Cover Today
Asian Grocery Williamsburg Va
R&J Travel And Tours Calendar
AI-Powered Free Online Flashcards for Studying | Kahoot!
Ksu Sturgis Library
Koninklijk Theater Tuschinski
Top 25 E-Commerce Companies Using FedEx
Infinite Campus Farmingdale
Shipping Container Storage Containers 40'HCs - general for sale - by dealer - craigslist
Yakini Q Sj Photos
Thotsbook Com
Collision Masters Fairbanks
Christie Ileto Wedding
Understanding & Applying Carroll's Pyramid of Corporate Social Responsibility
Palmyra Authentic Mediterranean Cuisine مطعم أبو سمرة
E. 81 St. Deli Menu
Latest Posts
Article information

Author: Kerri Lueilwitz

Last Updated:

Views: 5962

Rating: 4.7 / 5 (47 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Kerri Lueilwitz

Birthday: 1992-10-31

Address: Suite 878 3699 Chantelle Roads, Colebury, NC 68599

Phone: +6111989609516

Job: Chief Farming Manager

Hobby: Mycology, Stone skipping, Dowsing, Whittling, Taxidermy, Sand art, Roller skating

Introduction: My name is Kerri Lueilwitz, I am a courageous, gentle, quaint, thankful, outstanding, brave, vast person who loves writing and wants to share my knowledge and understanding with you.