From d7937be9edc2c9c079b6b80dc3d03080ae948941 Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Thu, 28 Aug 2025 16:37:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=80=80=E5=87=BA=E5=AF=BC?= =?UTF-8?q?=E8=88=AAbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../agent_service/AgentServiceImpl.swift | 1 + .../ios/Classes/AmapViewController.swift | 79 +++++++++++-------- .../chat_api/SystemFunctionHandler.kt | 2 +- .../chat_api/SystemFunctionHandler.swift | 29 ++++++- 4 files changed, 74 insertions(+), 37 deletions(-) diff --git a/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift b/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift index 347838118..cff6ff090 100644 --- a/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift +++ b/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift @@ -19,6 +19,7 @@ extension Notification.Name { static let navigationdEvent = Notification.Name("navigationdEvent") //导航事件 static let locationstartEvent = Notification.Name("locationstartEvent") //启动定位 static let updatelocationEvent = Notification.Name("updatelocationEvent") //更新位置事件 + static let exitnavigationdEvent = Notification.Name("exitnavigationdEvent") //退出导航事件 } class AgentServiceImpl: NSObject { private let TAG = "AgentServiceImpl" diff --git a/local_plugins/amap_service/ios/Classes/AmapViewController.swift b/local_plugins/amap_service/ios/Classes/AmapViewController.swift index 49092abc2..c6bda6a36 100644 --- a/local_plugins/amap_service/ios/Classes/AmapViewController.swift +++ b/local_plugins/amap_service/ios/Classes/AmapViewController.swift @@ -92,38 +92,39 @@ class AmapViewController: UIViewController { } private func setupAMapComponents() { + self.addNSNotification(); // 1. 初始化 DriveView(必须在主线程) - print("导航------------------ setupAMapComponents_1") +// print("导航------------------ setupAMapComponents_1") DispatchQueue.main.async { - print("导航------------------ setupAMapComponents_2") +// print("导航------------------ setupAMapComponents_2") self.driveView = AMapNaviDriveView(frame: self.view.bounds) - print("导航------------------ setupAMapComponents_3") +// print("导航------------------ setupAMapComponents_3") self.driveView.autoresizingMask = [.flexibleWidth, .flexibleHeight] - print("导航------------------ setupAMapComponents_4") +// print("导航------------------ setupAMapComponents_4") self.driveView.delegate = self - print("导航------------------ setupAMapComponents_5") +// print("导航------------------ setupAMapComponents_5") self.view.addSubview(self.driveView) - print("导航------------------ setupAMapComponents_6") +// print("导航------------------ setupAMapComponents_6") // 2. 初始化 DriveManager self.driveManager = AMapNaviDriveManager.sharedInstance() - print("导航------------------ setupAMapComponents_7") +// print("导航------------------ setupAMapComponents_7") self.driveManager.delegate = self - print("导航------------------ setupAMapComponents_8") +// print("导航------------------ setupAMapComponents_8") self.driveManager.allowsBackgroundLocationUpdates = true - print("导航------------------ setupAMapComponents_9") +// print("导航------------------ setupAMapComponents_9") // 3. 确保对象非空再关联 if let manager = self.driveManager, let view = self.driveView { - print("导航------------------ setupAMapComponents_10") +// print("导航------------------ setupAMapComponents_10") manager.addDataRepresentative(view) - print("导航------------------ setupAMapComponents_11") +// print("导航------------------ setupAMapComponents_11") self.startNavigation() - print("导航------------------ setupAMapComponents_12") +// print("导航------------------ setupAMapComponents_12") } else { print("Error: driveManager or driveView is nil") } - print("导航------------------ setupAMapComponents_13") +// print("导航------------------ setupAMapComponents_13") } } @@ -144,26 +145,38 @@ class AmapViewController: UIViewController { } @objc private func exitNavigation() { - print("导航------------------ 退出导航,清理资源") - // 停止导航 - driveManager.stopNavi() - driveManager.removeDataRepresentative(driveView) - driveManager.delegate = nil - - // 移除导航视图 - driveView.delegate = nil - driveView.removeFromSuperview() - driveView = nil - - // 销毁导航管理器 - driveManager = nil - - // 关闭当前页面,视情况而定 - // 如果你用的是导航控制器,可以pop - navigationController?.popViewController(animated: true) - // 或者如果是present出来的,dismiss - // dismiss(animated: true, completion: nil) - } + print("导航------------------ 退出导航,清理资源") + // 停止导航 + driveManager.stopNavi() + driveManager.removeDataRepresentative(driveView) + driveManager.delegate = nil + + // 移除导航视图 + driveView.delegate = nil + driveView.removeFromSuperview() + driveView = nil + + // 销毁导航管理器 + driveManager = nil + + // 关闭当前页面 - 修复:使用正确的退出方式 + if let navigationController = navigationController { + // 如果是在导航控制器中,使用 pop + navigationController.popViewController(animated: true) + } else { + // 如果是 present 出来的,使用 dismiss + dismiss(animated: true, completion: nil) + } + } + private func addNSNotification(){ + NotificationCenter.default.addObserver( + forName: NSNotification.Name("ExitNavigation"), + object: nil, + queue: .main + ) { _ in + self.exitNavigation() + } + } } // MARK: - 代理方法 diff --git a/local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/SystemFunctionHandler.kt b/local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/SystemFunctionHandler.kt index adacdafd9..af83a46d0 100644 --- a/local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/SystemFunctionHandler.kt +++ b/local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/SystemFunctionHandler.kt @@ -164,7 +164,7 @@ class SystemFunctionHandler(private val context: Context? = null) { handler = OpenRecorderHandler(context) ) - // 注册退出交互函数 + // 退出导航 client.registerLocalFunction( name = "exit_navigation", description = "结束当前导航服务", diff --git a/local_plugins/chat_api/ios/chat_api/Sources/chat_api/SystemFunctionHandler.swift b/local_plugins/chat_api/ios/chat_api/Sources/chat_api/SystemFunctionHandler.swift index 6f2bcf72d..60ec78415 100644 --- a/local_plugins/chat_api/ios/chat_api/Sources/chat_api/SystemFunctionHandler.swift +++ b/local_plugins/chat_api/ios/chat_api/Sources/chat_api/SystemFunctionHandler.swift @@ -64,9 +64,11 @@ public class SystemFunctionHandler: NSObject { do { // 注册退出交互函数 registerExitInteractionFunction(client: client) + // 注册退出导航模式 + registerExitNavigationFunction(client: client) // 注册翻译模式函数 - registerTranslationModeFunction(client: client) +// registerTranslationModeFunction(client: client) // 注册发送短信函数 registerSendTextMessageFunction(client: client) @@ -119,7 +121,22 @@ public class SystemFunctionHandler: NSObject { handler: handler ) } - + /// 注册退出交互函数 + private func registerExitNavigationFunction(client: MCPClient) { + let parameters: [String: Any] = [ + "type": "object", + "properties": [:], + "required": [] + ] + + let handler = ExitNavigationHandler() + _ = client.registerLocalFunction( + name: "exit_navigation", + description: "结束当前导航", + parameters: parameters, + handler: handler + ) + } /// 注册翻译模式函数 private func registerTranslationModeFunction(client: MCPClient) { let parameters: [String: Any] = [ @@ -558,7 +575,13 @@ private class ExitInteractionHandler: FunctionHandler { return "{\"result\": \"已结束当前交互\"}" } } - +/// 退出交互处理器 +private class ExitNavigationHandler: FunctionHandler { + func handle(arguments: [String: Any]) async throws -> String { + NotificationCenter.default.post(name: NSNotification.Name("ExitNavigation"), object: nil) + return "{\"result\": \"已结束当前导航\"}" + } +} /// 翻译模式处理器 private class TranslationModeHandler: FunctionHandler { func handle(arguments: [String: Any]) async throws -> String {