Maps SDK for Android 公用程式設定
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本頁面說明如何設定 Maps SDK for Android 公用程式庫,以及執行程式庫中的試用版應用程式。
必要條件和注意事項
Maps SDK for Android 公用程式庫支援 15 以上的 Android API 級別。設定專案版本目標時,請務必使用最新的 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
目錄中的公用程式庫。
- 各種檔案,包含授權、貢獻者和 README 資訊。
- Gradle 建構設定,可與 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 的應用程式。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2024-08-13 (世界標準時間)。
[null,null,["上次更新時間: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**."]]