You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.4 KiB

import Foundation
import NotificationCenter
extension Notification.Name {
static let navigationdEvent = Notification.Name("navigationdEvent") //导航事件
}
@objc class AmapHelper: NSObject {
private static var isInitialized = false
// MARK: - 初始化监听器(你可以在服务底层初始化时调用这个方法)
@objc static func initializeListener() {
guard !isInitialized else {
print("⚠️ AmapHelper 已经初始化过了,忽略重复调用")
return
}
isInitialized = true
print("✅ AmapHelper 初始化监听导航事件")
NotificationCenter.default.addObserver(
self,
selector: #selector(handleNavigationEvent(_:)),
name: .navigationdEvent, // 自定义事件名
object: nil
)
}
// MARK: - 处理导航事件
@objc static func handleNavigationEvent(_ notification: Notification) {
guard let userInfo = notification.userInfo else {
print("⚠️ 导航事件未携带 userInfo")
return
}
print("📍 收到导航事件 userInfo: \(userInfo)")
// 示例处理:读取起点和终点信息
if let start = userInfo["start"] as? String,
let end = userInfo["end"] as? String {
print("🚗 导航从 \(start) 到 \(end)")
// TODO: 调用实际导航逻辑
}
}
}