建立自訂指引

本頁說明如何建立自訂指引,做為自訂導覽體驗的一部分。

這個程序與「規劃路線」一文所述的程序不同,差異如下:

  • 您首先要獨立建立導覽工作階段,並透過工作階段取得導覽器執行個體,而不是呼叫檢視畫面控制器。
  • 您可以設定事件監聽器,回應及管理導覽事件。
  1. 使用 GMSNavigationService.createNavigationSession 建立導覽工作階段,並透過 setDestination 呼叫開始導覽。Google 導航體驗會透過導航地圖檢視畫面叫用導航器,而 GMSNavigationServices獨立於 UI 執行個體,控制導航工作階段並接收事件串流。這表示它可以不使用 UI 執行,也可以傳遞至任何以 UI 為基礎的體驗。採用這種做法後,導覽工作階段會繼續在應用程式中執行,直到從中移除最後一個參照為止。
  2. 建立路徑對齊位置資訊提供者。如果希望應用程式持續監控位置資訊,例如沿著路線顯示藍點的導覽檢視畫面,請使用位置資訊供應器。
  3. 實作 GMSNavigatorListener 通訊協定,為詳細的逐步導覽設定監聽器。然後將該資訊轉換為自訂導覽體驗所需的任何內容。例如:
    1. 實作純文字欄位,方便使用者輕鬆投放路線資訊。
    2. 設計及填入自有 UI 的欄位。
  4. 設定導覽模擬器。這是開發和測試的必要步驟。

建立獨立的導覽工作階段

以下是試用版中的程式碼片段,顯示與檢視控制器無關的導覽設定。接著,程式碼會新增總覽地圖,並設定顯示目前道路對齊位置。

// Create the navigation session.

 _navigationSession = [GMSNavigationServices createNavigationSession];
 GMSRoadSnappedLocationProvider *roadSnappedLocationProvider =
     _navigationSession.roadSnappedLocationProvider;
 [roadSnappedLocationProvider startUpdatingLocation];
 GMSNavigator *navigator = _navigationSession.navigator;
 [navigator addListener:self];
 navigator.voiceGuidance = GMSNavigationVoiceGuidanceSilent;
 navigator.sendsBackgroundNotifications = NO;
 _navigationSession.started = YES;
​​ [navigator setDestinations:@[ destination ]
                   callback:^(GMSRouteStatus routeStatus) {
                      // …handle changes in route status.
                    }];

 // Add an overview map.
 _mapView = [[GMSMapView alloc] initWithFrame:CGRectZero];
 [self.mainStackView addArrangedSubview:_mapView];
 [self.mainStackView setNeedsLayout];
 _mapView.settings.compassButton = YES;
 _mapView.delegate = self;
 _mapView.myLocationEnabled = YES;
 _mapView.roadSnappedMyLocationSource = roadSnappedLocationProvider;

將導覽從自訂體驗傳遞至 Google 體驗

這段程式碼片段說明應用程式如何讓使用者從自訂導覽體驗進入 Google 導覽體驗。這個程式碼片段也會顯示應用程式如何在分享地圖時進行這項轉換。

`UIButton *button = [UIButton buttonWithType:UIButtonTypePlain`];

[`button addTarget:self action:@selector(didTapEnterGoogleNavigationButton:)
forControlState:[_directionsButton addTarget:self`];

`…`

[`_mapView enableNavigationWithSession:_navigationSession`];