建立自訂指引
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本頁說明如何建立自訂指引,做為自訂導覽體驗的一部分。
這個程序與「規劃路線」一文所述的程序不同,差異如下:
- 您首先要獨立建立導覽工作階段,並透過工作階段取得導覽器執行個體,而不是呼叫檢視畫面控制器。
- 您可以設定事件監聽器,回應及管理導覽事件。
- 使用
GMSNavigationService.createNavigationSession
建立導覽工作階段,並透過 setDestination
呼叫開始導覽。Google 導航體驗會透過導航地圖檢視畫面叫用導航器,而 GMSNavigationServices
會獨立於 UI 執行個體,控制導航工作階段並接收事件串流。這表示它可以不使用 UI 執行,也可以傳遞至任何以 UI 為基礎的體驗。採用這種做法後,導覽工作階段會繼續在應用程式中執行,直到從中移除最後一個參照為止。
- 建立路徑對齊位置資訊提供者。如果希望應用程式持續監控位置資訊,例如沿著路線顯示藍點的導覽檢視畫面,請使用位置資訊供應器。
- 實作
GMSNavigatorListener
通訊協定,為詳細的逐步導覽設定監聽器。然後將該資訊轉換為自訂導覽體驗所需的任何內容。例如:
- 實作純文字欄位,方便使用者輕鬆投放路線資訊。
- 設計及填入自有 UI 的欄位。
- 設定導覽模擬器。這是開發和測試的必要步驟。
建立獨立的導覽工作階段
以下是試用版中的程式碼片段,顯示與檢視控制器無關的導覽設定。接著,程式碼會新增總覽地圖,並設定顯示目前道路對齊位置。
// 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`];
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-09-04 (世界標準時間)。
[null,null,["上次更新時間:2025-09-04 (世界標準時間)。"],[[["\u003cp\u003eCreate custom turn-by-turn navigation experiences on iOS by establishing an independent navigation session using \u003ccode\u003eGMSNavigationServices\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eImplement the \u003ccode\u003eGMSNavigatorListener\u003c/code\u003e protocol to receive and utilize detailed guidance information for your custom UI elements.\u003c/p\u003e\n"],["\u003cp\u003eUtilize a road-snapped location provider for continuous location updates and accurate positioning on the route.\u003c/p\u003e\n"],["\u003cp\u003eSet up a navigation simulator for development and testing purposes to ensure the functionality of your custom navigation experience.\u003c/p\u003e\n"],["\u003cp\u003eTransition seamlessly from a custom navigation experience to the full Google Maps navigation experience, optionally sharing the existing map instance.\u003c/p\u003e\n"]]],["The core process involves creating a navigation session independently using `GMSNavigationService.createNavigationSession`, distinct from route navigation. A road-snapped location provider is established for continuous monitoring. Implement `GMSNavigatorListener` to set up turn-by-turn guidance, translating the information into your app's custom navigation UI. Set up a navigation simulator for testing. The navigation session persists until the last reference is removed. A custom experience can also be transition to the default google navigation experience.\n"],null,["# Create customized guidance\n\nThis page covers the high-level steps you follow to create customized guidance\nas part of a [custom navigation\nexperience](/maps/documentation/navigation/ios-sdk/intro-custom-nav). \n**This process differs from the process described in\n[Navigate a\nroute](/maps/documentation/navigation/ios-sdk/route) as follows:**\n\n- You first establish a navigation session independently and obtain a navigator instance through the session rather than by calling the view controller.\n- You set up an event listener to respond to and manage navigation events.\n\n1. **Create a navigation session** using [`GMSNavigationService.createNavigationSession`](/maps/documentation/navigation/ios-sdk/reference/objc/Classes/GMSNavigationServices#+createnavigationsession) and begin navigation with a `setDestination` call. Where the Google navigation experience invokes the navigator through the navigation map view, the `GMSNavigationServices` controls and receives a stream of events from a navigation session *independently* from a UI instance. This means it can either run without a UI, or get passed to *any* UI-based experience. With this approach, the navigation session continues to run in your app until the last reference is removed from it.\n2. **Establish a road-snapped location provider**. Use the location provider if you want your app to have continuous location monitoring, such as when displaying a navigation view with a blue dot along the route.\n3. **Set up a listener for detailed turn-by-turn guidance** by implementing the `GMSNavigatorListener` protocol. Then, transform that information into whatever is needed for your custom navigation experience. For example:\n 1. Implement text-only fields for simple screen casting of directions.\n 2. Design and populate fields for your own UI.\n4. **Set up a navigation simulator**. This is necessary for development and testing.\n\nCreate an independent navigation session\n----------------------------------------\n\nThe following code snippet from the demo shows navigation established\nindependently from the view controller. The code then adds an overview map\nconfigured to show the current road-snapped location. \n\n // Create the navigation session.\n\n _navigationSession = [GMSNavigationServices createNavigationSession];\n GMSRoadSnappedLocationProvider *roadSnappedLocationProvider =\n _navigationSession.roadSnappedLocationProvider;\n [roadSnappedLocationProvider startUpdatingLocation];\n GMSNavigator *navigator = _navigationSession.navigator;\n [navigator addListener:self];\n navigator.voiceGuidance = GMSNavigationVoiceGuidanceSilent;\n navigator.sendsBackgroundNotifications = NO;\n _navigationSession.started = YES;\n [navigator setDestinations:@[ destination ]\n callback:^(GMSRouteStatus routeStatus) {\n // ...handle changes in route status.\n }];\n\n // Add an overview map.\n _mapView = [[GMSMapView alloc] initWithFrame:CGRectZero];\n [self.mainStackView addArrangedSubview:_mapView];\n [self.mainStackView setNeedsLayout];\n _mapView.settings.compassButton = YES;\n _mapView.delegate = self;\n _mapView.myLocationEnabled = YES;\n _mapView.roadSnappedMyLocationSource = roadSnappedLocationProvider;\n\nPassing navigation from a custom experience to the Google experience\n--------------------------------------------------------------------\n\nThis code snippet illustrates how your app can allow the user to enter the\nGoogle navigation experience from a custom navigation experience. This code\nsnippet also shows how your app makes this transition while sharing the map. \n\n `UIButton *button = [UIButton buttonWithType:UIButtonTypePlain`];\n\n [`button addTarget:self action:@selector(didTapEnterGoogleNavigationButton:)\n forControlState:[_directionsButton addTarget:self`];\n\n `...`\n\n [`_mapView enableNavigationWithSession:_navigationSession`];"]]