0

Earlier versions of Android Studio allowed the developer to create a project using the Google Maps Activity template as detailed on Google's Maps SDK for Android Quickstart. However, Android Studio Flamingo does not list a Google Maps Activity template when creating a new phone and tablet project:

enter image description here

So what should a developer do instead to create a Google Maps Activity?

tronman
  • 9,862
  • 10
  • 46
  • 61

3 Answers3

1

As you correctly pointed out, Android Studio Flamingo 2022.2.1 lacks the ability to create a Google Maps Activity. The link you also provided has a Look at the code section which shows the Activity code, Xml, and a dependency that needs to be added to your module level build.gradle.

Activity:

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
import com.google.android.gms.maps.model.MarkerOptions

internal class MapsActivity : 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)
    }

    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    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))
    }
}

Xml:

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

Module level build.gradle (Ensure that compileSdk is set to 31 or higher and minSdk is set to 19 or higher):

plugins {
    id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
    //...
}
...
dependencies {
    implementation 'com.google.android.gms:play-services-maps:18.1.0'
    // ...
}

Project-level build.gradle:

plugins {
    // ...
    id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' version '2.0.1' apply false
}

Top-level settings.gradle:

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}
// ...

local.properties:

MAPS_API_KEY=YOUR_API_KEY

AndroidManifest.xml:

<meta-data
    android:name="com.google.android.geo.API_KEY"
    android:value="${MAPS_API_KEY}" />
Victor Sklyarov
  • 827
  • 2
  • 3
  • 24
  • Yeah, that is just a whole lot of configuration that a programmer has to slog through. I was hoping for something simpler, especially for new Android programmers. I'm really curious why Google removed the template. – tronman May 05 '23 at 21:52
  • The thing I've heard is that Google is looking to develop compose more actively for use in new projects. Since the Google Maps Activity template was made in xml, I would venture to suggest that perhaps we will see it in future releases of Android Studio and it will be built on the basis of compose – Victor Sklyarov May 05 '23 at 22:03
1

Android Studio Flamingo 2022.2.1 does not provide Google Maps Activity project from the start, however you may start New Google Maps Views Activity after you created new project.

Steps are:

  1. Start New Empty Project
  2. Right Click on the package where you want to put you Activity
  3. New -> Google -> Google Maps Views Activity

Do not forget to get your Google Map API key from https://console.cloud.google.com/ before running your project.

Hope that helps.

0

So what should a developer do instead to create a Google Maps Activity?

You could follow the documentation that is not the quick start and shows you what to do without the template.

Or download an older version of studio if it's that important to you.

dominicoder
  • 9,338
  • 1
  • 26
  • 32