本指南列出了使用 Navigation SDK for Android 的建構設定需求。操作說明假設您已安裝 Android IDE,且熟悉 Android 開發作業。
使用 Navigation SDK 的基本需求
已啟用 Navigation SDK 的 Google Cloud 控制台專案。如需佈建,請洽詢 Google 地圖平台代表。
應用程式必須指定 Navigation SDK (16 版) 以上版本。
如要執行使用 Navigation SDK 建構的應用程式,Android 裝置必須安裝並啟用 Google Play 服務。
應用程式中必須加入作者資訊和授權文字。
設定專案:Google Cloud 控制台專案和 Android 專案
建構或測試應用程式前,您需要先建立 Cloud 控制台專案,並新增 API 金鑰憑證。該專案必須具有佈建功能,才能存取 Navigation SDK。Cloud 控制台專案中的所有金鑰都會取得同樣對 Navigation SDK 的存取權。一組金鑰可以有多個相關聯的開發專案。如果您已有控制台專案,就可以將金鑰新增至目前的專案。
如何設定
- 使用您慣用的網路瀏覽器 (例如 Chrome) 登入 Google Cloud 控制台,並建立 Google Cloud 控制台專案。
- 在 IDE (例如 Android Studio) 中建立 Android 應用程式開發專案,並記下套件名稱。
- 請與 Google 地圖平台代表聯絡,要求對方為 Google Cloud 控制台專案提供 Navigation SDK 存取權。
- 在網路瀏覽器的 Google Cloud 控制台資訊主頁中,建立憑證以產生設有限制的 API 金鑰。
- 在 API 金鑰頁面的「*應用程式限制」部分中,按一下「Android 應用程式」。
- 按一下「Add the package name and fingerprint」(新增套件名稱和指紋),然後輸入開發專案的套件名稱和該金鑰的 SHA-1 指紋。
- 點選「儲存」。
在應用程式中新增 Navigation SDK
Navigation SDK 以套件的形式提供。建立開發專案後,您可以整合 SDK。這些操作說明假設您使用 Android Studio 執行 IDE。
下載並解壓縮 Navigation SDK ZIP 檔案。
在 Android Studio 中開啟專案,並使用 SDK Manager 新增 Google Play 服務套件。
從 ZIP 檔案目錄,將
libs/google_navigation.aar
複製到專案的app/libs
目錄。
設定建構
建立專案後,您可以設定成功建構及使用 Navigation SDK 的設定。
更新店面房源
- 在 Gradle Scripts 資料夾中,開啟
local.properties
檔案並新增android.useDeprecatedNdk=true
。
更新 Gradle 建構指令碼
開啟
build.gradle (Module:app)
檔案,然後按照下列指南更新設定,以符合 Navigation SDK 的要求,並考慮一併設定最佳化選項。Navigation SDK 的必要設定
- 將
minSdkVersion
設為 16 以上。 - 為 API 21 設定「targetSDKversion」,將權限處理方式轉移到 API。針對先前的版本,您必須在應用程式中明確加入權限流程。
- 新增會調高
javaMaxHeapSize
的dexOptions
設定。 - 設定其他程式庫的位置。
- 為 Navigation SDK 新增
repositories
和dependencies
。 - 將依附元件中的版本號碼替換為最新可用版本。
縮短建構時間的選用設定
- 將
以下提供應用程式的 Gradle 建構指令碼範例。
apply plugin: 'com.android.application'
ext {
supportVersion = "27.1.1"
lifecycle_version = "1.1.1"
}
android {
compileSdkVersion 27
buildToolsVersion '28.0.3'
defaultConfig {
applicationId "com.example.navigationapidemo"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
resConfigs "en"
}
dexOptions {
// This increases the amount of memory available to the dexer. This is
// required to build apps using the Navigation SDK.
javaMaxHeapSize "4g"
}
buildTypes {
all {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
// Specify where to find additional libraries and the location of google_navigation.aar file.
repositories {
flatDir {
dirs 'libs'
}
google()
}
dependencies {
api fileTree(include: ['*.jar'], dir: 'libs')
// Include the Google Navigation API library
api(name: 'google_navigation', ext: 'aar')
// These dependencies are required for the Navigation API to function
// properly at runtime.
api "org.chromium.net:cronet-fallback:69.3497.100"
// Optional for Cronet users:
// api "org.chromium.net:cronet-api:69.3497.100"
api "com.android.support:appcompat-v7:${supportVersion}"
api "com.android.support:cardview-v7:${supportVersion}"
api "com.android.support:design:${supportVersion}"
api "com.android.support:mediarouter-v7:${supportVersion}"
api "com.android.support:preference-v7:${supportVersion}"
api "com.android.support:recyclerview-v7:${supportVersion}"
api 'com.github.bumptech.glide:glide:4.9.0'
api 'com.github.bumptech.glide:okhttp-integration:4.9.0'
api "android.arch.lifecycle:common-java8:$lifecycle_version"
api 'com.google.android.datatransport:transport-api:2.2.0'
api 'com.google.android.datatransport:transport-backend-cct:2.2.0'
api 'com.google.android.datatransport:transport-runtime:2.2.0'
api 'joda-time:joda-time:2.9.9'
// The Navigation SDK does not require the
// Places API; however, the demo app for does.
api 'com.google.android.gms:play-services-places:10.2.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
}
更新應用程式的資訊清單
在資訊清單資料夾中開啟
AndroidManifest.xml
檔案。在
<application>
元素中加入 API 金鑰。您必須使用 Google Cloud 控制台專案中定義的 API 金鑰,如上述步驟所述。在以下範例中顯示的部分資訊清單中,您應將
YOUR_API_KEY
換成自己的 API 金鑰:<meta-data android:name="com.google.android.geo.API_KEY" android:value="YOUR_API_KEY"/>
以下完整資訊清單顯示 API 金鑰的設定,以及在啟動期間執行的
MainActivity
的空白intent
。<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.navigationapidemo" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/_AppTheme" > <meta-data android:name="com.google.android.geo.API_KEY" android:value="YOUR_API_KEY"/> <activity android:name=".MainActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> ```
在應用程式中加入必要的作者資訊
如果您在應用程式中使用 Android 版 Navigation SDK,必須在應用程式的法律聲明部分加入作者資訊文字和開放原始碼授權。
您可以在 Navigation SDK for Android ZIP 檔案中找到所需的作者資訊文字和開放原始碼授權:
NOTICE.txt
LICENSES.txt
後續步驟
瞭解如何繪製路線。