MapAmapThemeControl.swift 27 KB

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