发送导航信息
为当前路线步骤创建 NavigationInfoModel,并将其发送到设备。
前置条件
- 设备已连接并遵循
TurnByTurnNavigationAPI。 instruction和roadName分别不能超过 128 字节。- 距离和时间应使用模型文档规定的单位。
API 参考
框架
AIBuds.xcframework
协议
- Swift
- Objective-C
/// Turn by turn navigation API
protocol TurnByTurnNavigationAPI: DeviceAPI {
/// Send navigation info
/// - Parameters:
/// - info: Navigation info
/// - completion: A closure that is called when the operation completes.
/// - success: `true` if the operation was successful; otherwise `false`.
/// - statusCode: The status code returned by the device. `nil` if the operation failed.
/// - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
func sendNavigationInfo(
_ info: NavigationInfoModel,
completion: AIBudsStatusCodeCompletionHandler?
)
}/// Turn by turn navigation API
@protocol AIBudsTurnByTurnNavigationAPI <AIBudsDeviceAPI>
/// Send navigation info
/// - Parameters:
/// - info: Navigation info
/// - completion: Reports success, device status code, and an optional error.
- (void)sendNavigationInfo:(AIBudsNavigationInfoModel *_Nonnull)info
completion:(AIBudsStatusCodeCompletionHandler _Nullable)completion;
@end请在 API Reference 中查看 sendNavigationInfo。
导航模型
| 属性 | 含义 |
|---|---|
nextStepDistanceText | 可直接显示的文本,例如 300m 或 1.5km。 |
maneuver | 由导航指令定义的原始值。 |
instruction | 导航指令文本,最多 128 字节。 |
roadName | 道路名称文本,最多 128 字节。 |
alertInfo | 可选的 NavigationAlertInfoModel;请按导航提醒中的取值与参数规则设置。 |
mapProvider | 由 NavigationMapProvider 定义的原始值。 |
transportMode | 由 NavigationTransportMode 定义的原始值。 |
remainingTime | 剩余时间,单位为秒。 |
remainingDistance | 剩余距离,单位为米。 |
使用示例
- Swift
- Objective-C
guard let device = device as? TurnByTurnNavigationAPI else { return }
let info = NavigationInfoModel()
info.nextStepDistanceText = "300m"
info.maneuver = NSNumber(value: NavigationManeuver.right.rawValue)
info.instruction = "Turn right"
info.roadName = "Market Street"
info.remainingTime = 420
info.remainingDistance = 1800
device.sendNavigationInfo(info) { success, statusCode, error in
guard success else {
print(error?.localizedDescription ?? "Navigation update failed")
return
}
print("Navigation sent: \(statusCode?.stringValue ?? "Unavailable")")
}id<AIBudsTurnByTurnNavigationAPI> device = (id<AIBudsTurnByTurnNavigationAPI>)self.device;
if (![device conformsToProtocol:@protocol(AIBudsTurnByTurnNavigationAPI)])
return;
AIBudsNavigationInfoModel *info = [[AIBudsNavigationInfoModel alloc] init];
info.nextStepDistanceText = @"300m";
info.maneuver = @(AIBudsNavigationManeuverRight);
info.instruction = @"Turn right";
info.roadName = @"Market Street";
info.remainingTime = @420;
info.remainingDistance = @1800;
[device
sendNavigationInfo:info
completion:^(BOOL success, NSNumber *_Nullable statusCode, NSError *_Nullable error) {
if (!success)
NSLog(@"Navigation update failed: %@", error.localizedDescription);
}];错误处理
发送前应验证字节长度。以 success 作为主要执行结果,并保留设备返回的 statusCode;公开 SDK 当前尚未说明各状态码的具体含义。