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 中,選取「檔案」>「新增」>「匯入專案」。
前往您儲存存放區的位置,然後選取 Kotlin 或 Java 的專案目錄:
- Kotlin:
PATH-REPO/android-samples/ApiDemos/kotlin
- Java:
PATH-REPO/android-samples/ApiDemos/java
- 選取「開啟」。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 地圖。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2024-05-09 (世界標準時間)。
[null,null,["上次更新時間:2024-05-09 (世界標準時間)。"],[[["\u003cp\u003eThis page provides access to the Maps SDK for Android sample code and apps, including a comprehensive ApiDemos app containing various map feature examples.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers 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.\u003c/p\u003e\n"],["\u003cp\u003eThe ApiDemos app showcases key features such as basic maps, markers, events, polylines, polygons, and My Location, with code samples readily available for use.\u003c/p\u003e\n"],["\u003cp\u003eTroubleshooting guidance is provided if the map doesn't display, directing users to verify their API key configuration.\u003c/p\u003e\n"],["\u003cp\u003eA separate sample app specifically for Wear OS is available on GitHub, demonstrating how to integrate Google Maps into wearable applications.\u003c/p\u003e\n"]]],["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"],null,["Select platform: [Android](/maps/documentation/android-sdk/examples \"View this page for the Android platform docs.\") [iOS](/maps/documentation/ios-sdk/examples \"View this page for the iOS platform docs.\") [JavaScript](/maps/documentation/javascript/examples \"View this page for the JavaScript platform docs.\")\n\n\u003cbr /\u003e\n\nThe Maps SDK for Android repository on [GitHub](https://github.com/googlemaps-samples/android-samples)\nincludes samples illustrating the use of the Maps SDK for Android in\nyour Android app.\n\nThe repository contains the [ApiDemos](https://github.com/googlemaps-samples/android-samples/tree/main/ApiDemos) app, a sample app that lets\nyou build and run the samples from a single Android app. Import and build the\napp, add your API key, view the demo, and use the sample code supplied as a\nstarting point for your app.\n\nWhen you run the sample app, it displays a list of available samples that you\ncan run on your own device. Select one of the options. For example, click\n**Basic Map**.\n\n**Troubleshooting:** If the sample app runs successfully but you don't see a\nmap, check that you've added your API key to the app's manifest file, as\ndescribed in [Using API Keys](/maps/documentation/android-sdk/get-api-key).\n\nMaps SDK for Android sample app list\n\nKey sample activities contained in the Maps SDK for Android\n[ApiDemos](https://github.com/googlemaps-samples/android-samples/tree/main/ApiDemos) app are listed below and reproduced at the linked pages\nfor quick reference. See the [Kotlin](https://github.com/googlemaps-samples/android-samples/tree/main/ApiDemos/kotlin/app/src/main/java/com/example/kotlindemos) or [Java](https://github.com/googlemaps-samples/android-samples/tree/main/ApiDemos/java/app/src/main/java/com/example/mapdemo)\nfolder for the full list of available samples.\n\n- [Basic map](/maps/documentation/android-sdk/examples/basic-map)\n- [Markers](/maps/documentation/android-sdk/examples/marker)\n- [Advanced markers](/maps/documentation/android-sdk/examples/advanced-marker)\n- [My Location](/maps/documentation/android-sdk/examples/my-location)\n- [Events](/maps/documentation/android-sdk/examples/events)\n- [Polylines](/maps/documentation/android-sdk/examples/polyline)\n- [Polygons](/maps/documentation/android-sdk/examples/polygon)\n\nClone and run the samples\n\nGit is required to run this sample locally. The following command clones the sample\napplication repository. \n\n```\ngit clone git@github.com:googlemaps-samples/android-samples.git\n```\n\nImport the sample project into Android Studio:\n\n1. In Android Studio, select **File \\\u003e New \\\u003e Import Project**.\n2. Go to the location where you saved the repository and select the project directory for\n Kotlin or Java:\n\n - **Kotlin** : \u003cvar translate=\"no\"\u003ePATH-REPO\u003c/var\u003e`/android-samples/ApiDemos/kotlin`\n - **Java** : \u003cvar translate=\"no\"\u003ePATH-REPO\u003c/var\u003e`/android-samples/ApiDemos/java`\n3. Select **Open**. Android Studio builds your project, using the Gradle build tool.\n4. Create a blank `secrets.properties` file in the same directory as your project's `local.properties` file. For more information about this file, see [Add your API key to the project](/maps/documentation/android-sdk/config#step_3_add_your_api_key_to_the_project).\n5. [Get an API key](/maps/documentation/android-sdk/get-api-key) from your project with the [Maps SDK for Android enabled](/maps/documentation/android-sdk/cloud-setup#enabling-apis).\n6. Add the following string to `secrets.properties`, replacing **YOUR_API_KEY** with the value of\n your API key:\n\n ```scdoc\n MAPS_API_KEY=YOUR_API_KEY\n ```\n7. Run the app.\n\nSample app for maps on Wear OS\n\nA sample app for Maps SDK for Android on Wear OS is\navailable on [GitHub](https://github.com/googlemaps-samples/android-samples/tree/main/WearOS), which you can use as a starting point when\nbuilding a wearable app. The sample shows you how to set up a basic Google Map\non Wear OS."]]