早期版本的 Android Studio 允许开发人员使用 Google Maps Activity 模板创建项目,详见 Google 的 Maps SDK for Android Quickstart。然而,Android Studio Flamingo 在创建新的手机和平板项目时并没有列出 Google Maps Activity 模板:
那么开发人员应该怎么做才能创建 Google Maps Activity?
正如您正确指出的那样,
Android Studio Flamingo 2022.2.1
缺乏创建Google Maps Activity 的能力。您还提供的链接有一个 Look at the code 部分,其中显示了 Activity
代码、Xml
和需要添加到模块级别的依赖项 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" />
模块级别
build.gradle
(确保 compileSdk
设置为 31 或更高 并且 minSdk
设置为 19 或更高):
plugins {
id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
//...
}
...
dependencies {
implementation 'com.google.android.gms:play-services-maps:18.1.0'
// ...
}
项目级
build.gradle
:
plugins {
// ...
id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin' version '2.0.1' apply false
}
顶级
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}" />
Android Studio Flamingo 2022.2.1 一开始不提供 Google Maps Activity 项目,但是您可以在创建新项目后启动 New Google Maps Views Activity。
步骤是:
在运行项目之前,不要忘记从 https://console.cloud.google.com/ 获取您的 Google Map API 密钥。
希望有帮助。