请选择平台Android iOS

配置实时异常路况

实时路况异常是一组功能,可提醒用户注意路线上的异常路况,并允许用户报告和验证遇到的异常路况。异常路况的示例包括交通事故、交通拥堵、警察和测速相机、施工、车道封闭以及某些天气状况。本页介绍了实时异常路况功能及其配置选项,包括使用自定义导航界面的应用需要考虑的事项。

实时异常路况功能

Navigation SDK 包含以下实时异常路况功能,这些功能是核心导航体验的一部分:

这些功能是可配置的,并且默认处于启用状态。以下部分详细介绍了这些功能和可用的配置选项。

沿途的互动式异常路况标注

当应用显示路线时(无论是在路线概览中还是在有效导航期间),任何当前的异常路况都会以标注的形式显示在路线上。标注包含一个图标,用于指示异常路况的类型。

路线沿途的标注

您可以使用 shouldDisplayPrompts 控制沿途异常路况标注的显示,该属性还控制在用户接近异常路况时自动提醒的 显示

mapView.navigator.shouldDisplayPrompts = true

在用户点按标注时显示异常路况详细信息

用户可以点按标注来显示信息卡片,其中包含有关异常路况的更多信息,包括异常路况类型、上次报告的时间,以及在某些情况下,用于投票表决异常路况是否仍然存在的选项。可能会显示两种不同类型的信息卡 ,具体取决于用户是否处于有效导航状态,并且每种类型的配置 选项各不相同。

在开始有效导航之前,路线概览中的标注信息卡

在开始有效导航之前,当用户点按路线概览中的标注时,系统会显示一个信息卡片,其中包含有关异常路况的更多信息。

“概览”信息卡片

您可以使用 showsIncidentCards控制用户是否能够点按路线概览中的异常路况标注来显示更多 信息。

mapView.settings.showsIncidentCards = true

在有效导航期间的标注信息卡

当异常路况标注在有效导航期间沿路线显示时,用户可以点按该标注来显示信息卡片,其中包含有关异常路况的更多信息,包括异常路况类型和上次报告的时间,以及用于投票表决异常路况是否仍然存在的按钮。用户提交的投票由 Google 处理,可能会在 Google 地图上向其他 Google 地图用户和 Navigation SDK 用户显示,并用于确定是否继续显示异常路况。

正在进行导航的信息卡片

您可以使用 shouldDisplayPrompts控制在有效导航期间异常路况标注的显示和可点按性,该属性还控制 沿途标注的显示以及在用户接近异常路况时 自动提醒的显示

mapView.navigator.shouldDisplayPrompts = true

在有效导航期间,自动异常路况提醒和投票

在有效导航期间,当用户接近路线上的异常路况时,系统会显示一个提示,其中包含 有关异常路况的信息以及用于投票表决异常路况是否仍然存在的按钮。用户提交的投票由 Google 处理,可能会在地图上向 其他 Google 地图和 Navigation SDK 用户显示,并用于确定是否继续显示异常路况。

正在进行导航的信息卡片

您可以使用 shouldDisplayPrompts配置在有效导航期间提醒提示的显示,该属性还控制 沿途标注的显示。

mapView.navigator.shouldDisplayPrompts = true

在有效导航期间报告异常路况

在有效导航模式下,导航界面上会显示一个按钮,用户可以通过该按钮报告路线上的新异常路况。当用户点按该按钮时,系统会显示一个菜单,其中包含可报告的异常路况类型。用户提交的报告由 Google 处理,可能会在地图上向其他 Google 地图和 Navigation SDK 用户显示。

“举报”按钮 报告菜单

显示或隐藏标准报告按钮

您可以使用 navigationReportIncidentButtonEnabled配置在有效导航期间标准报告按钮的可见性。

// Enables the incident reporting FAB to show in situations where incident
// reporting is possible.
mapView.settings.navigationReportIncidentButtonEnabled = true

添加自定义报告按钮

您可以向导航界面添加自定义报告按钮,以取代标准异常路况报告按钮。当用户点击自定义按钮时,您可以通过调用 presentReportIncidentsPanel 方法来触发报告菜单的显示。在添加自定义报告按钮之前,请先通过调用 reportIncidentsAvailable 验证应用是否处于有效导航状态,以及用户是否位于已启用报告功能的国家/地区。如果其中任何一个条件不成立,报告菜单将不会显示。

  // Check if reporting is available before displaying your button
  let isReportingAvailable = mapview.isIncidentReportingAvailable()
  customReportingIncidentButton.isHidden = !isReportingAvailable
  customReportingIncidentButton.addTarget(self, action: #selector(didTapReportIncidentButton), for: .touchUpInside)
  
  // Trigger the reporting flow if the button is clicked
  func didTapReportIncidentButton() {
          mapView.presentReportIncidentsPanel(self) { [weak self] error in
              guard let self = self else { return }
              if let error = error as NSError? {
                  if error.domain == GMSMapViewPresentReportIncidentPanelErrorDomain {
                      let errorCode = GMSMapViewPresentReportIncidentPanelErrorCode(rawValue: error.code)
                      
                      switch errorCode {
                      case .internal:
                          self.showErrorMessage(
                              title: "Error Presenting Report Incident Panel",
                              message: "An internal error occurred."
                          )
                      case .reportingNotAvailable:
                          self.showErrorMessage(
                              title: "Error Presenting Report Incident Panel",
                              message: "Reporting is not available."
                          )
                      case .none:
                          self.showErrorMessage(
                              title: "Error Presenting Report Incident Panel",
                              message: "An unknown error occurred."
                          )
                      }
                  } else {
                      // Handle other potential errors (e.g., network errors)
                      self.showErrorMessage(
                          title: "Error Presenting Report Incident Panel",
                          message: "An unexpected error occurred: \(error.localizedDescription)"
                      )
                  }
              }
              // If error is nil, the panel was presented successfully. You can add any extra logic here.
          }
     }

使用自定义导航界面

如果您的 Navigation SDK 实现包含自定义界面元素,您需要考虑实时异常路况元素,以避免冲突。

报告按钮定位

默认情况下,异常路况报告按钮位于地图的底部末端/尾随角,对于从左到右书写的语言,该按钮位于右侧;对于从右到左书写的语言,该按钮位于左侧。如果您需要移动报告按钮以留出空间放置自定义界面元素,请使用 bottomTrailingButtonsLayoutGuide

Swift

// Create a new layout guide
let topRightLayoutGuide = UILayoutGuide()
self.view.addLayoutGuide(topRightLayoutGuide)

// Activate constraints using fixed constants here as an example
// assuming the current reporting button is of fixed height
topRightLayoutGuide.topAnchor.constraint(equalTo: _mapView.navigationHeaderLayoutGuide.bottomAnchor, constant: 50).isActive = true
topRightLayoutGuide.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor, constant: -14).isActive = true

// Assign the layout guide
_mapView.bottomTrailingButtonsLayoutGuide = topRightLayoutGuide

// Create an alternate layout guide to use when the header and the footer are not full width
let topRightAlternateLayoutGuide = UILayoutGuide()
self.view.addLayoutGuide(topRightAlternateLayoutGuide)

// Activate constraints using fixed constants here as an example
// assuming the current RTD button is of fixed height
topRightAlternateLayoutGuide.topAnchor.constraint(equalTo: _mapView.navigationHeaderLayoutGuide.bottomAnchor, constant: 20).isActive = true
topRightAlternateLayoutGuide.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true

// Assign the layout guide
_mapView.bottomTrailingButtonsAlternateLayoutGuide = topRightAlternateLayoutGuide

Objective-C

// Create a new layout guide
UILayoutGuide *topRightLayoutGuide = [[UILayoutGuide alloc] init];
[self.view addLayoutGuide:topRightLayoutGuide];

// Activate constraints using fixed constants here as an example
// assuming the current RTD button is of fixed height
[[topRightLayoutGuide.topAnchor
    constraintEqualToAnchor:_mapView.navigationHeaderLayoutGuide.bottomAnchor
                   constant:50]
    setActive:YES];

[[topRightLayoutGuide.trailingAnchor
    constraintEqualToAnchor:self.view.safeAreaLayoutGuide.trailingAnchor
                   constant:-14]
    setActive:YES];

// Assign the layout guide
_mapView.bottomTrailingButtonsLayoutGuide = topRightLayoutGuide;

// Create an alternate layout guide to use when the header and the footer are not full width
UILayoutGuide *topRightAlternateLayoutGuide = [[UILayoutGuide alloc] init];
[self.view addLayoutGuide:topRightAlternateLayoutGuide];

// Activate constraints using fixed constants here as an example
// assuming the current RTD button is of fixed height
[[topRightAlternateLayoutGuide.topAnchor
    constraintEqualToAnchor:_mapView.navigationHeaderLayoutGuide.bottomAnchor
                   constant:50]
    setActive:YES];

[[topRightAlternateLayoutGuide.trailingAnchor
    constraintEqualToAnchor:self.view.safeAreaLayoutGuide.trailingAnchor
                   constant:-14]
    setActive:YES];

// Assign the layout guide
_mapView.bottomTrailingButtonsAlternateLayoutGuide = topRightAlternateLayoutGuide;

提示可见性 API(实验性)

提示可见性 API 可帮助您避免 Navigation SDK 生成的界面元素与您自己的自定义界面元素之间的冲突,方法是添加一个监听器,以便在 Navigation SDK 界面元素即将显示之前以及元素被移除后立即接收回调。您可以接收实时异常路况元素(包括信息卡、提示和异常路况报告菜单)以及 Navigation SDK 生成的其他通知的回调。

Swift

// Additional methods added to GMSNavigatorListener
...
func navigatorWillPresentPrompt(_ navigator: GMSNavigator) {
  // Hide any sort of custom UI element.
}

func navigatorDidDismissPrompt(_ navigator: GMSNavigator) {
  // Show any sort of custom UI element.
}
...

Objective-C

// Additional methods added to GMSNavigatorListener
...
- (void)navigatorWillPresentPrompt:(GMSNavigator *)navigator {
  // Hide any sort of custom UI element.
}

- (void)navigatorDidDismissPrompt:(GMSNavigator *)navigator {
  // Show any sort of custom UI element.
}
...