Browse Source

修复高德导航 退出 一直定位问题

newdev_shunjiawei
liwei1dao 1 year ago
parent
commit
35a133d266
  1. 2
      local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift
  2. 91
      local_plugins/amap_service/ios/Classes/AmapViewController.swift

2
local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift

@ -1508,7 +1508,7 @@ class AudioPlayer {
}
func playStartSound() {
// playSound(named: "start", fileType: "mp3")
playSound(named: "start", fileType: "mp3")
}
func playAwaitSound() {

91
local_plugins/amap_service/ios/Classes/AmapViewController.swift

@ -39,6 +39,58 @@ class AmapViewController: UIViewController {
setupAMapComponents()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// 页面即将消失时停止导航
if driveManager != nil {
driveManager.stopNavi()
driveManager.allowsBackgroundLocationUpdates = false
}
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
// 页面完全消失后清理资源
cleanupResources()
}
private func cleanupResources() {
print("开始清理导航资源")
// 停止语音合成
speechSynthesizer.stopSpeaking(at: .immediate)
// 如果导航管理器存在,进行清理
if let manager = driveManager {
// 停止导航
manager.stopNavi()
// 禁用后台定位
manager.allowsBackgroundLocationUpdates = false
// 清理代理
manager.delegate = nil
// 如果视图存在,移除数据代表
if let view = driveView {
manager.removeDataRepresentative(view)
view.delegate = nil
view.removeFromSuperview()
}
}
// 销毁单例实例(关键步骤)
AMapNaviDriveManager.destroyInstance()
// 清空引用
driveManager = nil
driveView = nil
print("导航资源清理完成")
}
private func setupAMapComponents() {
// 1. 初始化 DriveView(必须在主线程)
print("导航------------------ setupAMapComponents_1")
@ -118,16 +170,35 @@ class AmapViewController: UIViewController {
extension AmapViewController: AMapNaviDriveViewDelegate, AMapNaviDriveManagerDelegate {
//View回调
func driveViewCloseButtonClicked(_ driveView: AMapNaviDriveView) {
print("导航界面退出按钮被点击")
// 停止导航并清理
driveManager.stopNavi()
driveManager.removeDataRepresentative(driveView)
driveManager.delegate = nil
driveView.delegate = nil
driveView.removeFromSuperview()
// 返回到 Flutter 页面
self.dismiss(animated: true, completion: nil)
print("导航界面关闭按钮被点击")
// 1. 立即停止所有导航相关服务
driveManager?.stopNavi()
driveManager?.allowsBackgroundLocationUpdates = false
// 2. 清理视图和代理关系
driveManager?.removeDataRepresentative(driveView)
driveManager?.delegate = nil
driveView.delegate = nil
// 3. 从视图层次结构中移除
driveView.removeFromSuperview()
// 4. 停止语音合成
speechSynthesizer.stopSpeaking(at: .immediate)
// 5. 销毁导航管理器实例
AMapNaviDriveManager.destroyInstance()
// 6. 清空引用
driveManager = nil
// 7. 立即退出,不使用延迟
if let navigationController = navigationController {
navigationController.popViewController(animated: true)
} else {
dismiss(animated: true, completion: nil)
}
}
func driveView(_ driveView: AMapNaviDriveView, didChange showMode: AMapNaviDriveViewShowMode) {

Loading…
Cancel
Save