创建自定义指导

本页介绍了在自定义导航体验中创建自定义路线指引的大致步骤。

此过程与沿路线导航中所述的过程不同,具体如下:

  • 您首先需要独立建立导航会话,并通过该会话(而不是通过调用视图控制器)获取导航器实例。
  • 您需要设置一个事件监听器来响应和管理导航事件。
  1. 使用 GMSNavigationService.createNavigationSession 创建导航会话,并通过 setDestination 调用开始导航。在 Google 导航体验通过导航地图视图调用导航器的情况下,GMSNavigationServices 会独立于界面实例控制和接收来自导航会话的事件流。这意味着,它既可以在没有界面的情况下运行,也可以传递给任何基于界面的体验。采用这种方法时,导航会话会继续在应用中运行,直到最后一个引用从中移除。

  2. 建立道路吸附位置信息提供程序。如果您希望应用持续监控位置信息,例如在显示导航视图时沿路线显示蓝点,请使用位置信息提供程序。

  3. 通过实现 GMSNavigatorListener 协议,设置监听器以获取详细的精细导航。然后,将这些信息转换为自定义导航体验所需的任何内容。例如:

    1. 实现纯文本字段,以便简单地投射路线。
    2. 为自己的界面设计和填充字段。
  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`];