Khi bạn bật tính năng đi theo chỉ dẫn và đặt phương thức di chuyển thành lái xe, SDK điều hướng cho iOS sẽ hiển thị một nút điều khiển giới hạn tốc độ ở góc dưới bên trái của bản đồ, cho biết giới hạn tốc độ hiện tại. Nếu người lái xe vượt quá giới hạn tốc độ, thành phần điều khiển sẽ mở rộng để hiển thị đồng hồ tốc độ bên cạnh màn hình giới hạn tốc độ và kích hoạt cảnh báo khi tốc độ đạt đến một ngưỡng nhất định.
Theo mặc định, SDK Điều hướng sẽ kích hoạt cảnh báo tốc độ nhỏ khi người lái xe vượt quá giới hạn tốc độ 5 dặm/giờ (hoặc 10 km/giờ) và thay đổi màu của văn bản đồng hồ tốc độ thành màu đỏ. Ứng dụng này sẽ kích hoạt cảnh báo tốc độ cao khi người lái xe vượt quá giới hạn tốc độ 10 dặm/giờ (hoặc 20 km/giờ) và thay đổi màu nền của đồng hồ tốc độ thành màu đỏ.
Bạn có thể tuỳ chỉnh cả ngưỡng kích hoạt cảnh báo cũng như màu văn bản và màu nền mà đồng hồ tốc độ hiển thị. Bạn cũng có thể sử dụng SDK Điều hướng để cung cấp thông tin về tốc độ của tài xế. Ví dụ: bạn có thể cung cấp thông tin về tốc độ cho các nhà điều hành dịch vụ đi chung xe để giúp họ khuyến khích tài xế tuân thủ giới hạn tốc độ và cải thiện độ an toàn.
Tuỳ chỉnh ngưỡng cho cảnh báo tốc độ
Bạn có thể tuỳ chỉnh ngưỡng cảnh báo tốc độ cho cả cảnh báo tốc độ nhỏ và lớn dưới dạng tỷ lệ phần trăm vượt quá giới hạn tốc độ của tốc độ hiện tại. Bạn cũng có thể chỉ định khoảng thời gian vượt quá ngưỡng trước khi bản đồ hiển thị cảnh báo.
Ví dụ về mã sau đây đặt ngưỡng cho cảnh báo tốc độ nhỏ thành 5% so với giới hạn tốc độ và ngưỡng cho cảnh báo tốc độ lớn thành 10% so với giới hạn tốc độ. Nó chỉ định rằng bản đồ sẽ hiển thị cảnh báo sau khi ngưỡng cảnh báo bị vượt quá trong năm giây.
Swift
let minorSpeedAlertThresholdPercentage: CGFloat = 0.05 let
majorSpeedAlertThresholdPercentage: CGFloat = 0.1 let
severityUpgradeDurationSeconds: TimeInterval = 5
// Configure SpeedAlertOptions let mutableSpeedAlertOptions:
GMSNavigationMutableSpeedAlertOptions = GMSNavigationMutableSpeedAlertOptions()
mutableSpeedAlertOptions.setSpeedAlertThresholdPercentage(minorSpeedAlertThresholdPercentage,
for: .minor)
mutableSpeedAlertOptions.setSpeedAlertThresholdPercentage(majorSpeedAlertThresholdPercentage,
for: .major) mutableSpeedAlertOptions.severityUpgradeDurationSeconds =
severityUpgradeDurationSeconds
// Set SpeedAlertOptions to Navigator. mapView.navigator?.speedAlertOptions =
mutableSpeedAlertOptions; mapView.navigator?.add(self); // Only needed if
listening to the delegate events.
Objective-C
static const CGFloat minorSpeedAlertThresholdPercentage = 0.05; static const
CGFloat majorSpeedAlertThresholdPercentage = 0.1; static const NSTimeInterval
severityUpgradeDurationSeconds = 5;
// Configure SpeedAlertOptions GMSNavigationMutableSpeedAlertOptions
*mutableSpeedAlertOptions = [[GMSNavigationMutableSpeedAlertOptions alloc]
init]; [mutableSpeedAlertOptions setSpeedAlertThresholdPercentage:
minorSpeedAlertThresholdPercentage
forSpeedAlertSeverity:GMSNavigationSpeedAlertSeverityMinor];
[mutableSpeedAlertOptions
setSpeedAlertThresholdPercentage:majorSpeedAlertThresholdPercentage
forSpeedAlertSeverity:GMSNavigationSpeedAlertSeverityMajor];
[mutableSpeedAlertOptions
setSeverityUpgradeDurationSeconds:severityUpgradeDurationSeconds];
// Set SpeedAlertOptions to Navigator. mapView.navigator.speedAlertOptions =
mutableSpeedAlertOptions; [mapView.navigator addListener:self]; // Only needed
if listening to the delegate events.
Tuỳ chỉnh cách đồng hồ tốc độ hiển thị cảnh báo tốc độ
Bạn có thể tuỳ chỉnh màu sắc của màn hình đồng hồ tốc độ cho từng mức cảnh báo.
Bảng sau đây cho biết màu mặc định của cảnh báo tốc độ trong lớp GMSNavigationSpeedometerUIOptions:
| Phần tử | Màu |
|---|---|
| MinorSpeedAlertBackgroundColorDayMode | 0xffffff(trắng) |
| MinorSpeedAlertBackgroundColorNightMode | 0x000000 |
| MinorSpeedAlertTextColorDayMode | 0xd93025 |
| MinorSpeedAlertTextColorNightMode | 0xd93025 |
| MajorSpeedAlertBackgroundColorDayMode | 0xd93025 |
| MajorSpeedAlertBackgroundColorNightMode | 0xd93025 |
| MajorSpeedAlertTextColorDayMode | 0xffffff(trắng) |
| MajorSpeedAlertTextColorNightMode | 0xffffff(trắng) |
Bạn có thể chỉ định màu văn bản và màu nền của đồng hồ tốc độ cho cả cảnh báo tốc độ nhỏ và lớn:
Swift
let mutableSpeedometerUIOptions: GMSNavigationMutableSpeedometerUIOptions =
GMSNavigationMutableSpeedometerUIOptions()
mutableSpeedometerUIOptions.setTextColor(minorSpeedAlertTextColor, for: .minor,
lightingMode: .normal)
mutableSpeedometerUIOptions.setTextColor(majorSpeedAlertTextColor, for: .major,
lightingMode: .normal)
mutableSpeedometerUIOptions.setBackgroundColor(minorSpeedAlertNightModeBackgroundColor,
for: .minor, lightingMode: .lowLight)
mutableSpeedometerUIOptions.setBackgroundColor(majorSpeedAlertDayModeBackgroundColor,
for: .major, lightingMode: .normal)
mapView.settings.speedometerUIOptions = mutableSpeedometerUIOptions
Objective-C
GMSNavigationMutableSpeedometerUIOptions *mutableSpeedometerUIOptions =
[[GMSNavigationMutableSpeedometerUIOptions alloc] init];
[mutableSpeedometerUIOptions setTextColor: minorSpeedAlertTextColor
forSpeedAlertSeverity: GMSNavigationSpeedAlertSeverityMinor lightingMode:
GMSNavigationLightingModeNormal]; [mutableSpeedometerUIOptions setTextColor:
majorSpeedAlertTextColor forSpeedAlertSeverity:
GMSNavigationSpeedAlertSeverityMajor lightingMode:
GMSNavigationLightingModeNormal]; [mutableSpeedometerUIOptions
setBackgroundColor: minorSpeedAlertNightModeBackgroundColor
forSpeedAlertSeverity: GMSNavigationSpeedAlertSeverityMinor lightingMode:
GMSNavigationLightingModeLowLight]; [mutableSpeedometerUIOptions
setBackgroundColor: majorSpeedAlertDayModeBackgroundColor forSpeedAlertSeverity:
GMSNavigationSpeedAlertSeverityMajor
lightingMode:GMSNavigationLightingModeNormal];
mapView.settings.speedometerUIOptions = mutableSpeedometerUIOptions;
Nhận thông tin về tốc độ từ tài xế
Nếu ứng dụng của bạn yêu cầu chia sẻ thông tin về tốc độ của tài xế, thì bạn cũng có thể sử dụng SDK Điều hướng để cung cấp thông tin về tốc độ của tài xế. Điều này có thể hữu ích cho các ứng dụng đi chung xe, trong đó nhà điều hành có thể muốn giám sát hành vi chạy quá tốc độ của tài xế để cải thiện mức độ an toàn.
Ví dụ: ví dụ sau đây chia sẻ thông tin về tốc độ khi tốc độ vượt quá giới hạn tốc độ theo một tỷ lệ phần trăm nhất định:
Swift
// Listener method for sharing speed information when the speed exceeds the
speed limit by a specified percentage. #pragma mark GMSNavigatorListener func
navigator(_ navigator : GMSNavigator, didUpdateSpeedingPercentage
percentageAboveLimit : Float) { ... }
Objective-C
// Listener method listening to speeding feed. #pragma mark
GMSNavigatorListener - (void)navigator:(GMSNavigator *)navigator
didUpdateSpeedingPercentage:(float)percentageAboveLimit { ... }