設定 Xcode 專案

啟用計費功能並建立 API 金鑰後,您就可以設定 Xcode 您開發應用程式所用的專案。

版本資訊 版本。

步驟 1:安裝必要軟體

如要使用 Navigation SDK for iOS 建立專案,您必須下載並安裝:

  • Xcode 15.0 版或之後版本

步驟 2:建立 Xcode 專案並安裝 Navigation SDK

Swift 套件管理工具

可透過 安裝 Navigation SDK Swift 套件管理工具。如要新增 SDK,請確認您具備 已移除所有現有的 Navigation SDK 依附元件

如要將 SDK 加入新專案或現有專案,請按照下列步驟操作:

  1. 開啟 Xcode projectworkspace,然後依序前往「File」>新增套件依附元件
  2. 輸入 https://github.com/googlemaps/ios-navigation-sdk 做為網址,然後按下 Enter 鍵 來提取套件,然後點選「Add Package」
  3. 如要安裝特定 version,請將「Dependency Rule」(依附元件規則) 欄位設為 版本化選項如果是新專案,建議您指定最新版本 使用「完全比對版本」如果有需要 SQL 指令的分析工作負載 則 BigQuery 可能是最佳選擇完成後,點選「新增套件」。
  4. 在「Choose Package Products」視窗中,確認 GoogleNavigation 將新增至 您指定的main目標完成後,點選「新增套件」。
  5. 如要驗證安裝狀態,請前往目標的 General 窗格。 在「Frameworks、Library and Embedded Content」中應會顯示已安裝的套件。 您也可以查看「套件依附元件」「專案導覽器」的 驗證套件及其版本

如要更新現有專案的 package,請按照下列步驟操作:

  1. 如果要升級的是 9.0.0 以下版本,請務必移除 下列依附元件:GoogleMapsBaseGoogleMapsCore 和 升級後的費用為 GoogleMapsM4B。請勿移除以下項目的依附元件: GoogleMaps。詳情請參閱 9.0.0 版本資訊

    在 Xcode 專案配置設定中,找到「Frameworks, libraries」 和嵌入內容請使用減號(-) 移除下列架構:

    • GoogleMapsBase (僅適用於 9.0.0 以下版本的升級作業)
    • GoogleMapsCore (僅適用於 9.0.0 以下版本的升級作業)
    • GoogleMapsM4B (僅適用於 9.0.0 以下版本的升級作業)
  2. 在 Xcode 中,前往 [檔案] >套裝行程 >更新至最新套件版本」。
  3. 如要驗證安裝狀態,請前往「Project Navigator」的「Package Dependencies」部分 驗證套件及其版本
,瞭解如何調查及移除這項存取權。

如要移除使用 CocoaPods,請按照下列步驟操作:

  1. 關閉 Xcode 工作區。開啟終端機並執行下列指令:
    sudo gem install cocoapods-deintegrate cocoapods-clean 
    pod deintegrate 
    pod cache clean --all
  2. 移除 PodfilePodfile.resolved 和 如果不是 CocoaPods,則使用 Xcode workspace

如要移除已手動安裝的 iOS 版 Navigation SDK,請按照下列步驟操作: 步驟如下:
  1. 在 Xcode 專案配置設定中,找到「Frameworks, libraries」 和嵌入內容請使用減號(-) 移除下列架構:
    • GoogleMaps.xcframework
    • GoogleMapsBase.xcframework
    • GoogleMapsCore.xcframework
    • GoogleNavigation.xcframework
  2. 從 Xcode 專案的頂層目錄中,移除 GoogleMaps 軟體包。

CocoaPods

iOS 版 Navigation SDK 分為 CocoaPods Pod。 CocoaPods 是一種適用於 Swift 和 Objective-C Cocoa 專案的開放原始碼依附元件管理員。

如果您還沒有 CocoaPods 工具,請在終端機上執行下列指令,即可在 macOS 中安裝這項工具。詳情請參閱 CocoaPods 入門指南

sudo gem install cocoapods

建立適用於 iOS 的 Navigation SDK 的 Podfile 並使用 來安裝 API 及其依附元件:

  1. 如果還沒有 Xcode 專案,請立即建立並儲存到 本機電腦如果您是 iOS 開發新手:
    1. 建立新專案。
    2. 選取「iOS」>「App 範本。
    3. 在專案選項畫面中:
      1. 輸入專案名稱
      2. 記錄「Bundle ID」欄位的值。 您可以在下方使用該值限制 API 金鑰。
      3. 將專案「Interface」設為「Storyboard」
      4. 將「Language」(語言) 設為「Swift」或「Objective-C」
  2. 在專案目錄中建立名為 Podfile 的檔案。這個檔案定義了專案的依附元件。
  3. 編輯 Podfile,新增依附元件及其依附元件 版本。 以下範例包含以下項目所需的依附元件: Navigation SDK for iOS: # Cocoapods 安裝參考編號
    source 'https://github.com/CocoaPods/Specs.git'
    
    platform :ios, '15.0'
    
    target 'YOUR_APPLICATION_TARGET_NAME_HERE' do
      pod 'GoogleNavigation', '9.0.0'
    end
            
    請務必定期執行 pod outdated 以偵測更新 版本。必要時,請升級 至最新版本
  4. 儲存 Podfile
  5. 開啟終端機並前往包含 Podfile 的目錄:

    cd <path-to-project>
  6. 執行 pod install 指令。這會安裝 Podfile 中指定的 API 以及所有依附元件。

    pod install
  7. 關閉 Xcode,接著按兩下來開啟專案的 .xcworkspace 檔案來啟動 Xcode。從現在起,您必須使用 .xcworkspace 檔案來開啟專案。

如要更新現有專案的 API,請按照下列步驟操作:

  1. 開啟終端機,然後前往包含 Podfile 的專案目錄。
  2. 執行 pod update 指令。這會更新所有 API 在 Podfile 中指定的版本是最新版本。

手動安裝程式庫

本指南將說明如何手動新增包含 將 Navigation SDK for iOS 和 Maps SDK for iOS 加入您的專案,並在 Xcode 中設定建構設定。XCFramework 是一種二進位套件,可用於多種平台,包括使用 M1 晶片組的機器

請按照下列步驟安裝適用於 iOS 的 Navigation SDK 以及 Maps SDK for iOS:

  1. 下載以下 SDK 二進位檔和資源檔案:
  2. 啟動 Xcode,接著開啟現有專案,或建立新專案。如果您是 iOS 新手,請建立新專案並選取 iOS App Template
  3. 從專案中移除所有現有的地圖、導航和地點參照。
  4. 將下列 XCFrameworks 拖曳到專案下方的 Frameworks、Library 和 Embedded Content,以便安裝 Google 地圖 和 Navigation SDK (系統提示時,請選取「複製所需項目」):
    • GoogleMaps.xcframework
    • GoogleMapsBase.xcframework
    • GoogleMapsCore.xcframework
    • GoogleNavigation.xcframework
  5. 從下載的 GoogleMapsResources 物件中拖曳「GoogleMaps.bundle」 複製到 Xcode 專案的頂層目錄系統提示時 確認已選取「視需要複製項目」
  6. 從下載的 GoogleNavigationResources 中拖曳「GoogleNavigation.bundle」 複製到 Xcode 專案的頂層目錄系統提示時,請確認 已選取 [將項目複製到目的地群組的資料夾]
  7. 從「Project Navigator」中選取專案,然後選擇 並指定應用程式的目標
  8. 開啟「Build Phases」分頁, 將二進位檔連結至程式庫,新增下列架構和程式庫:
    • Accelerate.framework
    • AudioToolbox.framework
    • AVFoundation.framework
    • Contacts.framework
    • CoreData.framework
    • CoreGraphics.framework
    • CoreImage.framework
    • CoreLocation.framework
    • CoreTelephony.framework
    • CoreText.framework
    • GLKit.framework
    • ImageIO.framework
    • libc++.tbd
    • libxml2.tbd
    • libz.tbd
    • Metal.framework
    • OpenGLES.framework
    • QuartzCore.framework
    • Security.framework
    • SystemConfiguration.framework
    • UIKit.framework
    • UserNotifications.framework
    • WebKit.framework
  9. 在應用程式的目標中,選取「Capabilities」(功能) 分頁標籤。 開啟「背景模式」,並啟用下列模式:
    • 音訊、AirPlay 和子母畫面
    • 最新位置
  10. 選擇專案 (而非特定目標),然後開啟「Build」 設定標籤。在「Other Linker Flags」部分中 為 debugrelease 新增 ‑ObjC。 如果這些設定未顯示,請在 「Build Settings」列從「Basic」到「All」
  11. 開啟 Info.plist 並新增下列鍵/值組合:
    • 金鑰: NSLocationWhenInUseUsageDescription (隱私權 - 使用時位置說明)
      值:「這個應用程式需要相關權限,才能使用即時路線導航功能。」
    • 索引鍵: NSLocationAlwaysAndWhenInUseUsageDescription (隱私權 - 位置一律與使用時說明)
      值:「這個應用程式需要存取你的位置資訊,才能使用即時路線導航功能。」

步驟 3:檢查 Apple 隱私權資訊清單檔案

Apple 要求在 App Store 上架應用程式,要求取得應用程式隱私權詳細資訊。如需最新資訊和其他資訊,請前往 Apple App Store 隱私權詳細資料頁面

Apple 隱私權資訊清單檔案包含在 SDK 資源套件中。如要確認隱私權資訊清單檔案已納入並檢查其中的內容,請建立應用程式的封存檔案,然後從封存檔中產生隱私權報告

步驟 4:將 API 金鑰加進專案

以下範例說明如何在 Xcode 中將 API 金鑰加進專案:

Swift

將 API 金鑰新增到您的 AppDelegate.swift 中,如下所示:

  1. 新增下列匯入陳述式:
    import GoogleMaps
    import GoogleNavigation
  2. 請將以下內容新增到您的 application(_:didFinishLaunchingWithOptions:) 方法:
    GMSServices.provideAPIKey("YOUR_API_KEY")

Objective-C

將 API 金鑰新增到您的 AppDelegate.m 中,如下所示:

  1. 新增下列匯入陳述式:
    @import GoogleMaps;
    @import GoogleNavigation;
  2. 請將以下內容新增到您的 application:didFinishLaunchingWithOptions: 方法:
    [GMSServices provideAPIKey:@"YOUR_API_KEY"];

步驟 5:新增地圖

這段程式碼示範如何在現有的 ViewController 中加入簡易地圖, 包括瀏覽的一些初始設定

使用者必須同意相關條款和 條件。如要提示使用者,請呼叫 GMSNavigationServices.showTermsAndConditionsDialogIfNeeded(),然後查看 確認是否已接受條款。如果使用者拒絕條款 mapView.isNavigationEnabled = true 沒有作用,mapView.navigator 則為 nil。

Swift

import UIKit
import GoogleNavigation

class ViewController: UIViewController {

  var mapView: GMSMapView!
  var locationManager: CLLocationManager!

  override func loadView() {
    locationManager = CLLocationManager()
    locationManager.requestAlwaysAuthorization()

    let options = GMSMapViewOptions()
    options.camera = camera
    options.frame = .zero
    mapView = GMSMapView(options: options)

    // Show the terms and conditions.
    let termsAndConditionsOptions = GMSNavigationTermsAndConditionsOptions(companyName: "Ride Share Co.")

    GMSNavigationServices.showTermsAndConditionsDialogIfNeeded(with: termsAndConditionsOptions) { termsAccepted in
      if termsAccepted {
        // Enable navigation if the user accepts the terms.
        self.mapView.isNavigationEnabled = true
      } else {
        // Handle the case when the user rejects the terms and conditions.
      }
    }

    view = mapView
  }

  // TODO: Add navigation code.

}

Objective-C

#import "ViewController.h"
@import GoogleNavigation;

@interface ViewController ()
@end

@implementation ViewController
GMSMapView *_mapView;
CLLocationManager *_locationManager;

-   (void)loadView {

  _locationManager = [[CLLocationManager alloc] init];
  [_locationManager requestAlwaysAuthorization];

  GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:47.67
                                                          longitude:-122.20
                                                               zoom:14];
  GMSMapViewOptions *options = [[GMSMapViewOptions alloc] init];
  options.frame = CGRectZero;
  options.camera = camera;
  options.mapID = mapID;
  _mapView = [[GMSMapView alloc] initWithOptions:options];

  // Show the terms and conditions.
  GMSNavigationTermsAndConditionsOptions *termsAndConditionsOptions = [[GMSNavigationTermsAndConditionsOptions alloc] initWithCompanyName:@"Ride Sharing Co."];
  
  [GMSNavigationServices
    showTermsAndConditionsDialogIfNeededWithOptions:termsAndConditionsOptions
    callback:^(BOOL termsAccepted) {
     if (termsAccepted) {
       // Enable navigation if the user accepts the terms.
       _mapView.navigationEnabled = YES;
     } else {
       // Handle the case when the user rejects the terms and conditions.
     }
   }];

  self.view = _mapView;
}

// TODO: Add navigation code.

@end

執行您的應用程式。您應該會看到以華盛頓州柯克蘭為中心的地圖。

如果地圖無法顯示,請嘗試下列方法:

  • 請確認您提供的 API 金鑰正確無誤。
  • 檢查 Cloud 控制台是否已啟用 NavSDK。
  • 如果已安裝 Nav SDK,但專案並未使用 Nav SDK API,請將其從二進位檔中移除。

如果您是行動管理服務客戶

如果您是行動管理服務的客戶,請瞭解計費方式 。進一步瞭解錄製功能 交易,請參閱 設定帳單資訊。 如要瞭解如何在 Navigation SDK 實作中加入交易 ID,請參閱 將您的服務使用情形與行動交易建立關聯

後續步驟

取得 API 金鑰並設定 Xcode 專案後,接下來請建立並執行應用程式。 Navigation SDK for iOS 提供相關教學課程, 生成式 AI 模型相關詳情請參閱: