Google Maps Platform 即将推出全新地图样式。此次地图样式更新包括全新默认调色板,以及地图体验和易用性方面的改进。所有地图样式将于 2025 年 3 月自动更新。如需详细了解适用范围以及如何提前选择启用,请参阅
全新 Google Maps Platform 地图样式。
Maps SDK for Android 示例代码
GitHub 上的 Maps SDK for Android 代码库中包含一些示例,说明了如何在 Android 应用中使用 Maps SDK for Android。
该代码库中包含 ApiDemos 应用,这是一个示例应用,可让您通过单个 Android 应用构建和运行示例。您可以导入并构建应用、添加 API 密钥、查看演示版功能,并以提供的示例代码为基础开发您的应用。
当您运行示例应用时,它会显示一个包含可用示例的列表,您可以在自己的设备上运行这些示例。选择其中一个选项。例如,点击基本地图。
问题排查:如果示例应用可成功运行,但您看不到地图,请确认您已按使用 API 密钥中所述向应用的清单文件中添加了 API 密钥。
Maps SDK for Android 示例应用列表
下面列出了 Maps SDK for Android ApiDemos 应用中包含的关键示例活动,并且在链接页中进行了介绍,以便于您快速查看。有关可用示例的完整列表,请查看 Kotlin 或 Java 文件夹。
克隆并运行示例
您必须拥有 Git,才能在本地运行此示例。以下命令会克隆示例应用代码库。
git clone git@github.com:googlemaps-samples/android-samples.git
将示例项目导入到 Android Studio:
- 在 Android Studio 中,依次选择 File > New > Import Project。
前往代码库的保存位置并选择 Kotlin 或 Java 的项目目录:
- Kotlin:
PATH-REPO/android-samples/ApiDemos/kotlin
- Java:
PATH-REPO/android-samples/ApiDemos/java
- 选择 Open。Android Studio 会使用 Gradle 构建工具来构建您的项目。
- 在项目的
local.properties
文件所在的同一目录中,创建一个空白 secrets.properties
文件。如需了解详情,请参阅将您的 API 密钥添加到项目中。
在 secrets.properties
中添加以下字符串,并将 YOUR_API_KEY 替换为您的 API 密钥的值:
MAPS_API_KEY=YOUR_API_KEY
- 运行应用。
Wear OS 上地图的示例应用
GitHub 上提供了适用于 Wear OS 的 Maps SDK for Android 示例应用,您可以将其作为基础来开发穿戴式应用。该示例向您展示了如何在 Wear OS 上设置基本 Google 地图。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2024-05-09。
[null,null,["最后更新时间 (UTC):2024-05-09。"],[[["This page provides access to the Maps SDK for Android sample code and apps, including a comprehensive ApiDemos app containing various map feature examples."],["Developers can clone the sample repository from GitHub, import it into Android Studio, add their API key, and run the app to explore different map functionalities."],["The ApiDemos app showcases key features such as basic maps, markers, events, polylines, polygons, and My Location, with code samples readily available for use."],["Troubleshooting guidance is provided if the map doesn't display, directing users to verify their API key configuration."],["A separate sample app specifically for Wear OS is available on GitHub, demonstrating how to integrate Google Maps into wearable applications."]]],["The Maps SDK for Android samples are available on GitHub. To use them, clone the repository, import the ApiDemos project into Android Studio (either Kotlin or Java), and add your API key to the `secrets.properties` file. Run the app to view a list of samples like Basic Map, Markers, and Polylines. A separate Wear OS sample is also available on GitHub, showing basic map setup for wearable apps. Ensure your API key is correctly added to the app manifest if you encounter map display issues.\n"]]