Maps SDK for Android 实用程序设置
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本页面介绍如何设置 Maps SDK for Android 实用程序库,以及如何运行库中包含的演示版应用。
前提条件和说明
Maps SDK for Android 实用程序库支持 Android API 级别 15 及更高级别。您在设置项目 build 目标时,请务必使用最新的 Android API 级别。
GitHub 上提供了相关参考文档。
第 1 步:安装 Maps SDK for Android 实用程序库
将以下依赖项添加到应用的 build.gradle
文件:
dependencies {
// Utility Library for Maps SDK for Android
// You do not need to add a separate dependency for the Maps SDK for Android
// since this library builds in the compatible version of the Maps SDK.
implementation 'com.google.maps.android:android-maps-utils:3.8.0'
}
使用库中的某个实用程序,例如标记聚类、热图或其他实用程序。
第 2 步:(可选)安装 Maps SDK for Android 实用程序的 KTX 库
Maps SDK for Android 实用程序库附带了相应的 Kotlin 扩展程序 (KTX) 库。此库提供 Kotlin 语言功能(例如扩展函数、命名参数和默认参数),使您能够编写简洁而规范的 Kotlin 代码。如果您不使用 Kotlin 进行开发,则可以跳过此步骤。
要使用此库,除了 android-maps-utils
依赖项之外,还应将以下依赖项添加到应用的 build.gradle
文件中:
dependencies {
// KTX for the Maps SDK for Android Utility Library
implementation 'com.google.maps.android:maps-utils-ktx:5.1.1'
}
如需详细了解此库,请访问 Maps KTX GitHub 页面。
第 3 步:查看 Maps SDK for Android 实用程序库的源代码
由于该实用程序库是开源的,因此您可以将 android-maps-utils 库下载到计算机上,以便对其进行自定义、查看演示版应用,或者单纯了解该库的工作原理。您可以通过克隆代码库(建议采用此方式以便接收自动更新)或下载 zip 文件来实现上述目的。如果您想自定义该库,则应复刻该代码库。
该代码库中包含:
- 一个演示版应用,位于
demo
目录中。
- 实用程序库,位于
library
目录中。
- 各类包含许可、贡献者和自述文件信息的文件。
- Gradle build 配置,以用于 Android Studio。
运行实用程序演示版应用
实用程序库的 GitHub 代码库自带一个演示版应用,其中包含每个实用程序的实现代码示例。请按以下步骤在您的 Android 设备上运行演示版应用。
- 采用上述任一方式下载代码库。
- 在 Android Studio 中,依次选择 File -> New -> Import Project。
- 浏览到在您克隆实用程序库时创建的 android-maps-utils 目录,并选中(双击)它。现在,Android Studio 将构建该项目。
- 如果您使用的是 Android 设备而非模拟器,请务必启用开发者模式。然后将 Android 设备与计算机相连。
向 local.properties
添加一行代码,如下所示:
MAPS_API_KEY=YOUR_API_KEY
其中 YOUR_API_KEY 是您按照使用 API 密钥一文中的说明获取的 API 密钥。
- 构建并运行演示版应用。
此应用应在 Android 设备上打开,显示实用程序列表,包括聚类、polyutil、热图等。您还可以在应用列表中找到该应用,它在列表中的名称是 Maps Utils Demo。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2024-08-13。
[null,null,["最后更新时间 (UTC):2024-08-13。"],[[["\u003cp\u003eThis page explains how to set up the Maps SDK for Android utility library and run its demo app, allowing you to utilize features like marker clustering and heatmaps.\u003c/p\u003e\n"],["\u003cp\u003eThe library supports Android API level 15 and above, requiring you to add a specific dependency to your app's \u003ccode\u003ebuild.gradle.kts\u003c/code\u003e file for implementation.\u003c/p\u003e\n"],["\u003cp\u003eAn optional Kotlin extensions (KTX) library provides Kotlin-specific features for more concise coding if you are developing in Kotlin.\u003c/p\u003e\n"],["\u003cp\u003eThe open-source nature of the library allows for customization by cloning or downloading the \u003ccode\u003eandroid-maps-utils\u003c/code\u003e repository from GitHub.\u003c/p\u003e\n"],["\u003cp\u003eYou can explore the functionalities of the library by running the demo app included in the repository, requiring steps like adding your API key and building the project in Android Studio.\u003c/p\u003e\n"]]],["To set up the Maps SDK for Android utility library, add the `android-maps-utils` dependency to your `build.gradle.kts` file. Optionally, include the `maps-utils-ktx` dependency for Kotlin features. The library, which supports Android API level 15 and above, is open-source and can be downloaded from GitHub. To run the included demo app, clone the repository, import the project into Android Studio, enable developer mode on your Android device, add your API key to `local.properties`, and then build and run the app.\n"],null,["Select platform: [Android](/maps/documentation/android-sdk/utility/setup \"View this page for the Android platform docs.\") [iOS](/maps/documentation/ios-sdk/utility/setup \"View this page for the iOS platform docs.\")\n\nThis page describes how to set up the\n[Maps SDK for Android\nutility library](https://github.com/googlemaps/android-maps-utils), and to run the demo app included in the library.\n\nPrerequisites and notes\n\nThe Maps SDK for Android utility library supports Android\n[API\nlevel](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) 15 and above. When setting your project build target, make sure you use the latest\nAndroid API level.\n\nThe\n[reference\ndocumentation](http://googlemaps.github.io/android-maps-utils/javadoc/) is available on GitHub.\n\nStep 1. Install the Maps SDK for Android utility library\n\nAdd the following dependency to your app's `build.gradle.kts` file: \n\n```carbon\ndependencies {\n\n\n // Utility Library for Maps SDK for Android\n // You do not need to add a separate dependency for the Maps SDK for Android\n // since this library builds in the compatible version of the Maps SDK.\n implementation(\"com.google.maps.android:android-maps-utils:3.14.0\")\n}\n```\n\nUse one of the utilities from the library, such as\n[marker\nclustering](/maps/documentation/android-sdk/utility/marker-clustering),\n[heatmaps](/maps/documentation/android-sdk/utility/heatmap), or\nthe [other\nutilities](/maps/documentation/android-sdk/utility).\n\nStep 2. (Optional) Install the Maps SDK for Android utility KTX library\n\nThe Maps SDK for Android utility library comes with a corresponding Kotlin extensions\n(KTX) library. This library provides Kotlin language features---such as extension functions, named\nand default arguments---to enable you to write concise and idiomatic Kotlin. If you are not\ndeveloping in Kotlin, you can skip this step.\n\nTo use this library, add the following dependency, in addition to the\n`android-maps-utils` dependency, to your app's `build.gradle.kts` file: \n\n```carbon\ndependencies {\n\n // KTX for the Maps SDK for Android Utility Library\n implementation(\"com.google.maps.android:maps-utils-ktx:5.2.0\")\n}\n```\n\nCheck out the [Maps KTX GitHub](https://github.com/googlemaps/android-maps-ktx) page\nfor additional information about this library.\n\nStep 3. View the Maps SDK for Android utility library source code\n\nSince the utility library is open source, you can download the\n[android-maps-utils](https://github.com/googlemaps/android-maps-utils)\nlibrary to your machine to customize it, view the demo app, or if you just want to understand how\nthe library works. You can do so by\n[cloning](https://github.com/googlemaps/android-maps-utils.git) the repo (recommended,\nto receive automatic updates) or\n[downloading\na zip file](https://github.com/googlemaps/android-maps-utils/archive/master.zip). If you want to customize the library, you should\n[fork the repo](https://help.github.com/articles/fork-a-repo).\n\nThe repository includes:\n\n- A demo application, in the `demo` directory.\n- The library of utilities, in the `library` directory.\n- Various files containing license, contributors, and readme information.\n- Gradle build configuration, for use with Android Studio.\n\nRun the utility demo app\n\nThe GitHub repository for the utility library ships with a demo app that includes sample\nimplementations of each utility. Follow these steps to run the demo app on\nyour Android device.\n\n1. Download the repository using one of the options mentioned above.\n2. In Android Studio, choose **File -\\\u003e New -\\\u003e Import Project**.\n3. Browse to and select (double-click) the **android-maps-utils** directory that was created when you cloned the utility library. Android Studio now builds the project.\n4. If you are using an Android device instead of an emulator, make sure to [enable developer mode](https://developer.android.com/tools/device.html#setting-up). Then attach the Android device to your computer.\n5. Add a single line to `local.properties` that looks like this:\n\n ```scdoc\n MAPS_API_KEY=YOUR_API_KEY\n ```\n\n Where \u003cvar translate=\"no\"\u003eYOUR_API_KEY\u003c/var\u003e is the API key you obtained as described in\n [Using API Keys](/maps/documentation/android-sdk/get-api-key).\n6. Build and run the **demo** app.\n\nThe app should open on the Android device, displaying a list of utilities\nincluding clustering, poyutil, heatmap, and more. You can also find the app in\nyour app list, under the name **Maps Utils Demo**."]]