|
|
@ -92,38 +92,39 @@ class AmapViewController: UIViewController { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private func setupAMapComponents() { |
|
|
private func setupAMapComponents() { |
|
|
|
|
|
self.addNSNotification(); |
|
|
// 1. 初始化 DriveView(必须在主线程) |
|
|
// 1. 初始化 DriveView(必须在主线程) |
|
|
print("导航------------------ setupAMapComponents_1") |
|
|
// print("导航------------------ setupAMapComponents_1") |
|
|
DispatchQueue.main.async { |
|
|
DispatchQueue.main.async { |
|
|
print("导航------------------ setupAMapComponents_2") |
|
|
// print("导航------------------ setupAMapComponents_2") |
|
|
self.driveView = AMapNaviDriveView(frame: self.view.bounds) |
|
|
self.driveView = AMapNaviDriveView(frame: self.view.bounds) |
|
|
print("导航------------------ setupAMapComponents_3") |
|
|
// print("导航------------------ setupAMapComponents_3") |
|
|
self.driveView.autoresizingMask = [.flexibleWidth, .flexibleHeight] |
|
|
self.driveView.autoresizingMask = [.flexibleWidth, .flexibleHeight] |
|
|
print("导航------------------ setupAMapComponents_4") |
|
|
// print("导航------------------ setupAMapComponents_4") |
|
|
self.driveView.delegate = self |
|
|
self.driveView.delegate = self |
|
|
print("导航------------------ setupAMapComponents_5") |
|
|
// print("导航------------------ setupAMapComponents_5") |
|
|
self.view.addSubview(self.driveView) |
|
|
self.view.addSubview(self.driveView) |
|
|
print("导航------------------ setupAMapComponents_6") |
|
|
// print("导航------------------ setupAMapComponents_6") |
|
|
|
|
|
|
|
|
// 2. 初始化 DriveManager |
|
|
// 2. 初始化 DriveManager |
|
|
self.driveManager = AMapNaviDriveManager.sharedInstance() |
|
|
self.driveManager = AMapNaviDriveManager.sharedInstance() |
|
|
print("导航------------------ setupAMapComponents_7") |
|
|
// print("导航------------------ setupAMapComponents_7") |
|
|
self.driveManager.delegate = self |
|
|
self.driveManager.delegate = self |
|
|
print("导航------------------ setupAMapComponents_8") |
|
|
// print("导航------------------ setupAMapComponents_8") |
|
|
self.driveManager.allowsBackgroundLocationUpdates = true |
|
|
self.driveManager.allowsBackgroundLocationUpdates = true |
|
|
print("导航------------------ setupAMapComponents_9") |
|
|
// print("导航------------------ setupAMapComponents_9") |
|
|
|
|
|
|
|
|
// 3. 确保对象非空再关联 |
|
|
// 3. 确保对象非空再关联 |
|
|
if let manager = self.driveManager, let view = self.driveView { |
|
|
if let manager = self.driveManager, let view = self.driveView { |
|
|
print("导航------------------ setupAMapComponents_10") |
|
|
// print("导航------------------ setupAMapComponents_10") |
|
|
manager.addDataRepresentative(view) |
|
|
manager.addDataRepresentative(view) |
|
|
print("导航------------------ setupAMapComponents_11") |
|
|
// print("导航------------------ setupAMapComponents_11") |
|
|
self.startNavigation() |
|
|
self.startNavigation() |
|
|
print("导航------------------ setupAMapComponents_12") |
|
|
// print("导航------------------ setupAMapComponents_12") |
|
|
} else { |
|
|
} else { |
|
|
print("Error: driveManager or driveView is nil") |
|
|
print("Error: driveManager or driveView is nil") |
|
|
} |
|
|
} |
|
|
print("导航------------------ setupAMapComponents_13") |
|
|
// print("导航------------------ setupAMapComponents_13") |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -144,26 +145,38 @@ class AmapViewController: UIViewController { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@objc private func exitNavigation() { |
|
|
@objc private func exitNavigation() { |
|
|
print("导航------------------ 退出导航,清理资源") |
|
|
print("导航------------------ 退出导航,清理资源") |
|
|
// 停止导航 |
|
|
// 停止导航 |
|
|
driveManager.stopNavi() |
|
|
driveManager.stopNavi() |
|
|
driveManager.removeDataRepresentative(driveView) |
|
|
driveManager.removeDataRepresentative(driveView) |
|
|
driveManager.delegate = nil |
|
|
driveManager.delegate = nil |
|
|
|
|
|
|
|
|
// 移除导航视图 |
|
|
// 移除导航视图 |
|
|
driveView.delegate = nil |
|
|
driveView.delegate = nil |
|
|
driveView.removeFromSuperview() |
|
|
driveView.removeFromSuperview() |
|
|
driveView = nil |
|
|
driveView = nil |
|
|
|
|
|
|
|
|
// 销毁导航管理器 |
|
|
// 销毁导航管理器 |
|
|
driveManager = nil |
|
|
driveManager = nil |
|
|
|
|
|
|
|
|
// 关闭当前页面,视情况而定 |
|
|
// 关闭当前页面 - 修复:使用正确的退出方式 |
|
|
// 如果你用的是导航控制器,可以pop |
|
|
if let navigationController = navigationController { |
|
|
navigationController?.popViewController(animated: true) |
|
|
// 如果是在导航控制器中,使用 pop |
|
|
// 或者如果是present出来的,dismiss |
|
|
navigationController.popViewController(animated: true) |
|
|
// dismiss(animated: true, completion: nil) |
|
|
} 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: - 代理方法 |
|
|
// MARK: - 代理方法 |
|
|
|