MapAmapThemeControl.swift 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. //
  2. // MapAmapThemeControl.swift
  3. // map_amap_ios
  4. //
  5. // Created by 诺诺诺的言 on 2025/7/22.
  6. //
  7. import Foundation
  8. import MapKit
  9. import Combine
  10. import AMapFoundationKit
  11. import MAMapKit
  12. @available(iOS 13.0, *)
  13. class MapAmapThemeControl: UIViewController {
  14. required init?(coder: NSCoder) {
  15. fatalError("init(coder:) has not been implemented")
  16. }
  17. let viewModel: MapAmapViewAndDataExchange
  18. var autonaviMap : MAMapView!
  19. //let mapView = MKMapView()
  20. private var cancellables = Set<AnyCancellable>()
  21. init(viewModel: MapAmapViewAndDataExchange) {
  22. self.viewModel = viewModel
  23. super.init(nibName: nil, bundle: nil)
  24. //mapView.showsUserLocation = false
  25. }
  26. override func viewDidLoad() {
  27. super.viewDidLoad()
  28. // 1. 设置隐私(必须!)
  29. MAMapView.updatePrivacyShow(.didShow, privacyInfo: .didContain)
  30. MAMapView.updatePrivacyAgree(.didAgree)
  31. AMapServices.shared().enableHTTPS = true
  32. loadMainMapView()
  33. bindViewModel()
  34. }
  35. private func loadMainMapView() {
  36. guard AMapServices.shared().apiKey != nil else {
  37. fatalError("高德地图API Key未配置!")
  38. }
  39. autonaviMap = MAMapView(frame: view.bounds)
  40. print("地图初始化失败,请检查Key和Bundle ID--\(autonaviMap)")
  41. guard let map = autonaviMap else {
  42. print("地图初始化失败,请检查Key和Bundle ID--")
  43. return
  44. }
  45. map.delegate = self
  46. map.isRotateCameraEnabled = false
  47. map.showsCompass = false
  48. map.isRotateEnabled = false
  49. map.mapType = .standard
  50. map.showsUserLocation = false
  51. map.userTrackingMode = .none
  52. view.addSubview(map)
  53. map.translatesAutoresizingMaskIntoConstraints = false
  54. view.addSubview(map)
  55. NSLayoutConstraint.activate([
  56. map.topAnchor.constraint(equalTo: view.topAnchor),
  57. map.bottomAnchor.constraint(equalTo: view.bottomAnchor),
  58. map.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  59. map.trailingAnchor.constraint(equalTo: view.trailingAnchor)
  60. ])
  61. }
  62. private func bindViewModel() {
  63. viewModel.$markers
  64. .receive(on: DispatchQueue.main)
  65. .sink { [weak self] markers in
  66. if let annotationsList = self?.autonaviMap.annotations {
  67. print("annotationsListsdfsdfs---\(annotationsList)");
  68. self?.autonaviMap.removeAnnotations(self?.autonaviMap.annotations)
  69. }
  70. // UIView.animate(withDuration: 0.3) {
  71. // self?.autonaviMap.addAnnotations(markers)
  72. // }
  73. var mapMarkerList = [MAPointAnnotation]();
  74. for markerItem in markers {
  75. let annotationItem : MyMAPointAnnotation = MyMAPointAnnotation(itemMarker: markerItem)
  76. annotationItem.title = markerItem.markerName;
  77. annotationItem.coordinate = markerItem.coordinate;
  78. mapMarkerList.append(annotationItem)
  79. }
  80. self?.autonaviMap.addAnnotations(mapMarkerList)
  81. // self?.autonaviMap.selectAnnotation(mapMarkerList.first, animated: true)
  82. self?.autonaviMap.setZoomLevel(20, animated: true)
  83. /*if !mapMarkerList.isEmpty {
  84. let selectIemt : MAPointAnnotation = mapMarkerList.first!
  85. self?.autonaviMap.setCenter(selectIemt.coordinate, animated: true)
  86. }*/
  87. }
  88. .store(in: &cancellables)
  89. ///移动到特定的位置
  90. viewModel.$currentRegion
  91. .compactMap { $0 }
  92. .receive(on: DispatchQueue.main)
  93. .sink { [weak self] region in
  94. self?.autonaviMap.setRegion(region, animated: true)
  95. //self?.mapView.setRegion(region, animated: true)
  96. }
  97. .store(in: &cancellables)
  98. viewModel.$polylines
  99. .receive(on: DispatchQueue.main)
  100. .sink { [weak self] polylines in
  101. self!.autonaviMap.removeOverlays(self!.autonaviMap.overlays)
  102. guard let self, !polylines.isEmpty else { return }
  103. // 清除旧覆盖物
  104. self.autonaviMap.removeOverlays(self.autonaviMap.overlays)
  105. var polyinesList = [MAPolyline]();
  106. for polyline in polylines {
  107. let polylineItem : MyMAPolyline = MyMAPolyline(coordinates: &polyline.points, count: UInt(polyline.points.count))
  108. polylineItem.itemPolyLine = polyline
  109. polyinesList.append(polylineItem)
  110. }
  111. // 添加新覆盖物
  112. self.autonaviMap.addOverlays(polyinesList)
  113. // 自动调整视野
  114. if let lastPolyline = polylines.last,
  115. let padding = lastPolyline.mapPadding {
  116. self.autonaviMap.showOverlays(
  117. polyinesList,
  118. edgePadding: UIEdgeInsets(
  119. top: padding.top,
  120. left: padding.left,
  121. bottom: padding.bottom,
  122. right: padding.right
  123. ),
  124. animated: true
  125. )
  126. }
  127. /*var polyinesList = [MKPolyline]();
  128. for polyline in polylines {
  129. let polylineItem : MKPolyline = polyline.polyline
  130. polylineItem.associatedLineId = polyline.id
  131. polylineItem.associatedLineType = polyline.lineType
  132. polylineItem.associatedColor = polyline.color
  133. polylineItem.associatedWidth = polyline.width
  134. polyinesList.append(polylineItem)
  135. }
  136. self?.mapView.addOverlays(polyinesList)
  137. //self?.mapView.addOverlays(polylines.map({ $0.polyline }))
  138. if let padding = polylines.last?.mapPadding {
  139. self?.mapView.fitsAllPoints(
  140. points: polylines.last?.points.compactMap({ MKMapPoint($0) }) ?? [],
  141. padding: padding.padding,
  142. animated: true
  143. )
  144. }
  145. guard let self, !polylines.isEmpty else { return }
  146. // 清除旧覆盖物
  147. self.autonaviMap.removeOverlays(self.autonaviMap.overlays)
  148. // 创建自定义Polyline类数组
  149. let customPolylines = polylines.compactMap { polyline -> CustomPolyline? in
  150. // 创建高德地图Polyline
  151. let count = UInt(polyline.points.count)
  152. guard let mapPolyline = MAPolyline(coordinates: &polyline.points , count: count) else {
  153. return nil
  154. }
  155. // 使用自定义类包装
  156. return CustomPolyline(
  157. polyline: mapPolyline,
  158. id: polyline.id,
  159. color: polyline.color,
  160. width: polyline.width
  161. )
  162. }
  163. // 添加新覆盖物
  164. self.autonaviMap.addOverlays(customPolylines.map { $0.polyline })
  165. // 自动调整视野
  166. if let lastPolyline = polylines.last,
  167. let padding = lastPolyline.mapPadding {
  168. self.autonaviMap.showOverlays(
  169. customPolylines.map { $0.polyline },
  170. edgePadding: UIEdgeInsets(
  171. top: padding.top,
  172. left: padding.left,
  173. bottom: padding.bottom,
  174. right: padding.right
  175. ),
  176. animated: true
  177. )
  178. }*/
  179. }
  180. .store(in: &cancellables)
  181. //移动至多个点的位置,并提供设置padding距离
  182. viewModel.$suitableLocation
  183. .receive(on: DispatchQueue.main)
  184. .sink{ [weak self] polylines in
  185. if let padding = polylines.last?.mapPadding {
  186. if let atMapPoint : [CLLocationCoordinate2D] = polylines.last?.points {
  187. self?.autonaviMap.moveToSuitableLocation(points: atMapPoint, padding: padding)
  188. //self?.mapView.moveToSuitableLocation(points: atMapPoint, padding: padding)
  189. }
  190. }
  191. }
  192. .store(in: &cancellables)
  193. /*viewModel.$polylines
  194. .receive(on: DispatchQueue.main)
  195. .sink { [weak self] polylines in
  196. guard let self else { return }
  197. // 清除旧覆盖物
  198. self.autonaviMap.removeOverlays(self.autonaviMap.overlays)
  199. // 空数据检查
  200. guard !polylines.isEmpty else {
  201. print("轨迹数据为空")
  202. return
  203. }
  204. // 处理每条轨迹
  205. var validPolylines: [MAPolyline] = []
  206. for polyline in polylines {
  207. // 坐标数量检查
  208. guard !polyline.points.isEmpty else {
  209. print("发现空坐标的轨迹: \(polyline.id)")
  210. continue
  211. }
  212. // 指针转换
  213. let creationResult = polyline.points.withUnsafeBufferPointer { bufferPointer -> MAPolyline? in
  214. guard let baseAddress = bufferPointer.baseAddress, bufferPointer.count > 0 else {
  215. return nil
  216. }
  217. let unsafePointer = UnsafeMutablePointer(mutating: baseAddress)
  218. return MAPolyline(
  219. coordinates: unsafePointer,
  220. count: UInt(bufferPointer.count)
  221. )
  222. }
  223. // 结果检查
  224. guard let mapPolyline = creationResult else {
  225. print("轨迹创建失败: \(polyline.id)")
  226. continue
  227. }
  228. // 绑定属性
  229. mapPolyline.associatedData = [
  230. "id": polyline.id,
  231. "color": polyline.color,
  232. "width": polyline.width
  233. ]
  234. validPolylines.append(mapPolyline)
  235. }
  236. // 添加有效轨迹
  237. guard !validPolylines.isEmpty else {
  238. print("没有有效的轨迹数据")
  239. return
  240. }
  241. self.autonaviMap.addOverlays(validPolylines)
  242. // 调整视野
  243. if let lastValid = validPolylines.last,
  244. let padding = polylines.last?.mapPadding {
  245. self.autonaviMap.showOverlays(
  246. validPolylines,
  247. edgePadding: UIEdgeInsets(
  248. top: padding.top,
  249. left: padding.left,
  250. bottom: padding.bottom,
  251. right: padding.right
  252. ),
  253. animated: true
  254. )
  255. }
  256. }
  257. .store(in: &cancellables)
  258. viewModel.$markers
  259. .receive(on: DispatchQueue.main)
  260. .sink { [weak self] markers in
  261. self?.mapView.removeAnnotations(self?.mapView.annotations ?? [])
  262. UIView.animate(withDuration: 0.3) {
  263. self?.mapView.addAnnotations(markers)
  264. }
  265. }
  266. .store(in: &cancellables)
  267. viewModel.$polylines
  268. .receive(on: DispatchQueue.main)
  269. .sink { [weak self] polylines in
  270. self?.mapView.removeOverlays(self?.mapView.overlays ?? [])
  271. var polyinesList = [MKPolyline]();
  272. for polyline in polylines {
  273. let polylineItem : MKPolyline = polyline.polyline
  274. polylineItem.associatedLineId = polyline.id
  275. polylineItem.associatedLineType = polyline.lineType
  276. polylineItem.associatedColor = polyline.color
  277. polylineItem.associatedWidth = polyline.width
  278. polyinesList.append(polylineItem)
  279. }
  280. self?.mapView.addOverlays(polyinesList)
  281. //self?.mapView.addOverlays(polylines.map({ $0.polyline }))
  282. if let padding = polylines.last?.mapPadding {
  283. self?.mapView.fitsAllPoints(
  284. points: polylines.last?.points.compactMap({ MKMapPoint($0) }) ?? [],
  285. padding: padding.padding,
  286. animated: true
  287. )
  288. }
  289. }
  290. .store(in: &cancellables)
  291. //移动至多个点的位置,并提供设置padding距离
  292. viewModel.$suitableLocation
  293. .receive(on: DispatchQueue.main)
  294. .sink{ [weak self] polylines in
  295. if let padding = polylines.last?.mapPadding {
  296. if let atMapPoint : [CLLocationCoordinate2D] = polylines.last?.points {
  297. self?.mapView.moveToSuitableLocation(points: atMapPoint, padding: padding)
  298. }
  299. }
  300. }
  301. .store(in: &cancellables)*/
  302. }
  303. /*private func bindViewModel() {
  304. viewModel.$markers
  305. .receive(on: DispatchQueue.main)
  306. .sink { [weak self] markers in
  307. self?.autonaviMap.removeAnnotations(self?.autonaviMap.annotations)
  308. var mapMarkerList = [MAPointAnnotation]();
  309. for markerItem in markers {
  310. let annotationItem : MAPointAnnotation = MAPointAnnotation()
  311. annotationItem.title = markerItem.markerName;
  312. annotationItem.coordinate = markerItem.coordinate;
  313. mapMarkerList.append(annotationItem)
  314. }
  315. self?.autonaviMap.addAnnotations(mapMarkerList)
  316. self?.autonaviMap.selectAnnotation(mapMarkerList.first, animated: true)
  317. self?.autonaviMap.setZoomLevel(15.1, animated: true)
  318. let selectIemt : MAPointAnnotation = mapMarkerList.first!
  319. self?.autonaviMap.setCenter(selectIemt.coordinate, animated: true)
  320. //self?.mapView.removeAnnotations(self?.mapView.annotations ?? [])
  321. // UIView.animate(withDuration: 0.3) {
  322. // self?.mapView.addAnnotations(markers)
  323. // }
  324. }
  325. .store(in: &cancellables)
  326. viewModel.$currentRegion
  327. .compactMap { $0 }
  328. .receive(on: DispatchQueue.main)
  329. .sink { [weak self] region in
  330. self?.mapView.setRegion(region, animated: true)
  331. }
  332. .store(in: &cancellables)
  333. viewModel.$markers
  334. .receive(on: DispatchQueue.main)
  335. .sink { [weak self] markers in
  336. self?.mapView.removeAnnotations(self?.mapView.annotations ?? [])
  337. UIView.animate(withDuration: 0.3) {
  338. self?.mapView.addAnnotations(markers)
  339. }
  340. }
  341. .store(in: &cancellables)
  342. viewModel.$polylines
  343. .receive(on: DispatchQueue.main)
  344. .sink { [weak self] polylines in
  345. self?.mapView.removeOverlays(self?.mapView.overlays ?? [])
  346. var polyinesList = [MKPolyline]();
  347. for polyline in polylines {
  348. let polylineItem : MKPolyline = polyline.polyline
  349. polylineItem.associatedLineId = polyline.id
  350. polylineItem.associatedLineType = polyline.lineType
  351. polylineItem.associatedColor = polyline.color
  352. polylineItem.associatedWidth = polyline.width
  353. polyinesList.append(polylineItem)
  354. }
  355. self?.mapView.addOverlays(polyinesList)
  356. //self?.mapView.addOverlays(polylines.map({ $0.polyline }))
  357. if let padding = polylines.last?.mapPadding {
  358. self?.mapView.fitsAllPoints(
  359. points: polylines.last?.points.compactMap({ MKMapPoint($0) }) ?? [],
  360. padding: padding.padding,
  361. animated: true
  362. )
  363. }
  364. }
  365. .store(in: &cancellables)
  366. //移动至多个点的位置,并提供设置padding距离
  367. viewModel.$suitableLocation
  368. .receive(on: DispatchQueue.main)
  369. .sink{ [weak self] polylines in
  370. if let padding = polylines.last?.mapPadding {
  371. if let atMapPoint : [CLLocationCoordinate2D] = polylines.last?.points {
  372. self?.mapView.moveToSuitableLocation(points: atMapPoint, padding: padding)
  373. }
  374. }
  375. }
  376. .store(in: &cancellables)
  377. }*/
  378. }
  379. // 自定义Polyline包装类
  380. private class CustomPolyline {
  381. let polyline: MAPolyline
  382. let id: String
  383. let color: String
  384. let width: Double
  385. init(polyline: MAPolyline, id: String, color: String, width: Double) {
  386. self.polyline = polyline
  387. self.id = id
  388. self.color = color
  389. self.width = width
  390. }
  391. }
  392. private class MyMAPointAnnotation : MAPointAnnotation {
  393. let itemMarker : ATMapMarker
  394. init(itemMarker: ATMapMarker) {
  395. self.itemMarker = itemMarker
  396. }
  397. }
  398. private class MyMAPolyline : MAPolyline {
  399. var itemPolyLine : ATMapPolyline! = nil
  400. }
  401. @available(iOS 13.0, *)
  402. extension MapAmapThemeControl : MAMapViewDelegate {
  403. //根据anntation生成对应的View。
  404. func mapView(_ mapView: MAMapView!, viewFor annotation: MAAnnotation!) -> MAAnnotationView! {
  405. // guard let marker = annotation as? ATMapMarker else {
  406. // return nil
  407. // }
  408. if let myitemAnnotation = annotation as? MyMAPointAnnotation {
  409. if myitemAnnotation.itemMarker.markerType.isMapAchorPoint {
  410. let annotationView = (mapView.dequeueReusableAnnotationView(withIdentifier: MapAnnotationAnchorPointView.identifier) as? MapAnnotationAnchorPointView) ?? MapAnnotationAnchorPointView(annotation: (myitemAnnotation as MAAnnotation), reuseIdentifier: MapAnnotationAnchorPointView.identifier)
  411. annotationView!.marker = myitemAnnotation.itemMarker
  412. return annotationView
  413. } else if (myitemAnnotation.itemMarker.markerType.isTracePopup) {
  414. let annotationBubbleView = (mapView.dequeueReusableAnnotationView(withIdentifier: MapAmapPopUpWindowView.identifier) as? MapAmapPopUpWindowView) ?? MapAmapPopUpWindowView(annotation: (myitemAnnotation as MAAnnotation), reuseIdentifier: MapAmapPopUpWindowView.identifier)
  415. annotationBubbleView!.marker = myitemAnnotation.itemMarker
  416. return annotationBubbleView
  417. }
  418. let annotationView = (mapView.dequeueReusableAnnotationView(withIdentifier: MapAnnotationView.identifier) as? MapAnnotationView) ?? MapAnnotationView(annotation: (myitemAnnotation as MAAnnotation), reuseIdentifier: MapAnnotationView.identifier)
  419. annotationView!.marker = myitemAnnotation.itemMarker
  420. return annotationView
  421. }
  422. return nil
  423. /*if annotation is MyMAPointAnnotation,let myitemAnnotation = annotation as? MyMAPointAnnotation {
  424. let pointReuseIdentifier = "pointReuseIndetifier"
  425. var annotationView = mapView.dequeueReusableAnnotationView(
  426. withIdentifier: pointReuseIdentifier
  427. ) as? MAPinAnnotationView
  428. if annotationView == nil {
  429. annotationView = MAPinAnnotationView(
  430. annotation: annotation,
  431. reuseIdentifier: pointReuseIdentifier
  432. )
  433. }
  434. annotationView?.canShowCallout = true
  435. annotationView?.animatesDrop = true
  436. annotationView?.isDraggable = false
  437. annotationView?.pinColor = .purple
  438. return annotationView
  439. }
  440. return nil*/
  441. }
  442. func mapView(_ mapView: MAMapView!, rendererFor overlay: MAOverlay!) -> MAOverlayRenderer! {
  443. guard let polyline = overlay as? MyMAPolyline else {
  444. return MAPolylineRenderer(polyline: overlay as? MAPolyline)
  445. }
  446. // 外层边框(粗线)
  447. // let borderRenderer = MAPolylineRenderer(polyline: polyline)
  448. // borderRenderer?.lineWidth = 8
  449. // borderRenderer?.strokeColor = UIColor(red: 0.35, green: 0.36, blue: 0.99, alpha: 1)
  450. //
  451. let renderer = MAPolylineRenderer(polyline: polyline)
  452. renderer?.lineWidth = 12
  453. renderer?.lineCapType = kMALineCapRound//kMALineCapArrow
  454. renderer?.lineJoinType = kMALineJoinRound
  455. // 设置箭头样式
  456. // 加载箭头纹理图片
  457. //normal error selected color
  458. if polyline.itemPolyLine.lineType == "normal" {
  459. renderer?.strokeImage = readImageContentFrom(imageName: "com.shishi.dingwei_bluearrow")
  460. //renderer?.strokeColor = UIColor(red: 0.27, green: 0.46, blue: 1, alpha: 1)
  461. } else if polyline.itemPolyLine.lineType == "error" {
  462. //renderer?.strokeColor = UIColor(red: 1, green: 0.43, blue: 0.43, alpha: 1)
  463. renderer?.strokeImage = readImageContentFrom(imageName: "com.shishi.dingwei_redarrow")
  464. } else if polyline.itemPolyLine.lineType == "selected" {
  465. //renderer?.strokeColor = UIColor(red: 0.08, green: 0.8, blue: 0.63, alpha: 1)
  466. renderer?.strokeImage = readImageContentFrom(imageName: "com.shishi.dingwei_greetarrow")
  467. } else if polyline.itemPolyLine.lineType == "color" {
  468. renderer?.strokeColor = UIColor(red: 0.27, green: 0.46, blue: 1, alpha: 1)
  469. renderer?.lineWidth = polyline.itemPolyLine.width
  470. renderer?.sideColor = UIColor(red: 0.35, green: 0.36, blue: 0.99, alpha: 1)
  471. renderer?.is3DArrowLine = true
  472. renderer?.fillColor = UIColor(hex: polyline.itemPolyLine.color)//UIColor.init(hex: "#4476FF");//UIColor(red: 123 / 255, green: 125 / 255, blue: 255 / 255, alpha: 1)
  473. }
  474. return renderer
  475. }
  476. private func readImageContentFrom(imageName : String) -> UIImage {
  477. if let image = UIImage(named: imageName) {
  478. return image
  479. }
  480. // 尝试从插件资源束加载
  481. let bundleURL = Bundle(for: MapAnnotationView.self).url(forResource: "map_amap_ios_image_source", withExtension: "bundle")
  482. if let bundleURL = bundleURL, let resourceBundle = Bundle(url: bundleURL) {
  483. return UIImage(named: imageName, in: resourceBundle, compatibleWith: nil) ?? UIImage()
  484. }
  485. return UIImage()
  486. }
  487. }
  488. /*
  489. @available(iOS 13.0, *)
  490. extension MapAmapThemeControl: MKMapViewDelegate {
  491. func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
  492. guard let marker = annotation as? ATMapMarker else {
  493. return nil
  494. }
  495. print("markerTypesfdsdfs---\(marker.markerType.markType)");
  496. if marker.markerType.isMapAchorPoint {
  497. let annotationView = (mapView.dequeueReusableAnnotationView(withIdentifier: MapAnnotationAnchorPointView.identifier) as? MapAnnotationAnchorPointView) ?? MapAnnotationAnchorPointView(annotation: marker, reuseIdentifier: MapAnnotationAnchorPointView.identifier)
  498. annotationView.marker = marker
  499. annotationView.prepareForDisplay()
  500. return annotationView
  501. } else if (marker.markerType.isTracePopup) {
  502. let annotationBubbleView = (mapView.dequeueReusableAnnotationView(withIdentifier: MapAmapPopUpWindowView.identifier) as? MapAmapPopUpWindowView) ?? MapAmapPopUpWindowView(annotation: marker, reuseIdentifier: MapAmapPopUpWindowView.identifier)
  503. annotationBubbleView.marker = marker
  504. annotationBubbleView.prepareForDisplay()
  505. return annotationBubbleView
  506. }
  507. let annotationView = (mapView.dequeueReusableAnnotationView(withIdentifier: MapAnnotationView.identifier) as? MapAnnotationView) ?? MapAnnotationView(annotation: marker, reuseIdentifier: MapAnnotationView.identifier)
  508. annotationView.marker = marker
  509. annotationView.prepareForDisplay()
  510. return annotationView
  511. }
  512. func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
  513. guard var marker = view.annotation as? ATMapMarker else {
  514. return
  515. }
  516. viewModel.handleMarkerTap(marker: &marker)
  517. }
  518. func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
  519. guard let polyline = overlay as? MKPolyline else {
  520. return MKOverlayRenderer(overlay: overlay)
  521. }
  522. let renderer = MapAmapArrowPolylineRenderer(polyline: polyline)
  523. renderer.lineWidth = 6
  524. //rgba(123, 125, 255, 1)
  525. renderer.strokeColor = UIColor(red: 0.27, green: 0.46, blue: 1, alpha: 1)//UIColor.init(hex: "#4476FF");//UIColor(red: 123 / 255, green: 125 / 255, blue: 255 / 255, alpha: 1)
  526. renderer.lineCap = .round
  527. renderer.lineJoin = .round
  528. // 设置箭头样式
  529. renderer.arrowColor = UIColor.white // 箭头颜色
  530. renderer.arrowSize = 12 // 增大箭头大小
  531. renderer.arrowSpacing = 50 // 减小箭头间距
  532. renderer.borderWidth = 1
  533. renderer.borderColor = UIColor(red: 0.35, green: 0.36, blue: 0.99, alpha: 1)
  534. //normal error selected color
  535. if polyline.associatedLineType == "normal" {
  536. renderer.strokeColor = UIColor(red: 0.27, green: 0.46, blue: 1, alpha: 1)
  537. } else if polyline.associatedLineType == "error" {
  538. renderer.strokeColor = UIColor(red: 1, green: 0.43, blue: 0.43, alpha: 1)
  539. } else if polyline.associatedLineType == "selected" {
  540. renderer.strokeColor = UIColor(red: 0.08, green: 0.8, blue: 0.63, alpha: 1)
  541. } else if polyline.associatedLineType == "color" {
  542. renderer.strokeColor = UIColor(hex: polyline.associatedColor ?? "#4476FF")
  543. renderer.lineWidth = polyline.associatedWidth ?? 0
  544. }
  545. return renderer
  546. }
  547. }*/