MapAmapThemeControl.swift 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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 = .follow
  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. guard let self, !polylines.isEmpty else { return }
  94. // 清除旧覆盖物
  95. self.autonaviMap.removeOverlays(self.autonaviMap.overlays)
  96. var polyinesList = [MAPolyline]();
  97. for polyline in polylines {
  98. let polylineItem : MyMAPolyline = MyMAPolyline(coordinates: &polyline.points, count: UInt(polyline.points.count))
  99. polylineItem.itemPolyLine = polyline
  100. polyinesList.append(polylineItem)
  101. }
  102. // 添加新覆盖物
  103. self.autonaviMap.addOverlays(polyinesList)
  104. // 自动调整视野
  105. if let lastPolyline = polylines.last,
  106. let padding = lastPolyline.mapPadding {
  107. self.autonaviMap.showOverlays(
  108. polyinesList,
  109. edgePadding: UIEdgeInsets(
  110. top: padding.top,
  111. left: padding.left,
  112. bottom: padding.bottom,
  113. right: padding.right
  114. ),
  115. animated: true
  116. )
  117. }
  118. /*var polyinesList = [MKPolyline]();
  119. for polyline in polylines {
  120. let polylineItem : MKPolyline = polyline.polyline
  121. polylineItem.associatedLineId = polyline.id
  122. polylineItem.associatedLineType = polyline.lineType
  123. polylineItem.associatedColor = polyline.color
  124. polylineItem.associatedWidth = polyline.width
  125. polyinesList.append(polylineItem)
  126. }
  127. self?.mapView.addOverlays(polyinesList)
  128. //self?.mapView.addOverlays(polylines.map({ $0.polyline }))
  129. if let padding = polylines.last?.mapPadding {
  130. self?.mapView.fitsAllPoints(
  131. points: polylines.last?.points.compactMap({ MKMapPoint($0) }) ?? [],
  132. padding: padding.padding,
  133. animated: true
  134. )
  135. }
  136. guard let self, !polylines.isEmpty else { return }
  137. // 清除旧覆盖物
  138. self.autonaviMap.removeOverlays(self.autonaviMap.overlays)
  139. // 创建自定义Polyline类数组
  140. let customPolylines = polylines.compactMap { polyline -> CustomPolyline? in
  141. // 创建高德地图Polyline
  142. let count = UInt(polyline.points.count)
  143. guard let mapPolyline = MAPolyline(coordinates: &polyline.points , count: count) else {
  144. return nil
  145. }
  146. // 使用自定义类包装
  147. return CustomPolyline(
  148. polyline: mapPolyline,
  149. id: polyline.id,
  150. color: polyline.color,
  151. width: polyline.width
  152. )
  153. }
  154. // 添加新覆盖物
  155. self.autonaviMap.addOverlays(customPolylines.map { $0.polyline })
  156. // 自动调整视野
  157. if let lastPolyline = polylines.last,
  158. let padding = lastPolyline.mapPadding {
  159. self.autonaviMap.showOverlays(
  160. customPolylines.map { $0.polyline },
  161. edgePadding: UIEdgeInsets(
  162. top: padding.top,
  163. left: padding.left,
  164. bottom: padding.bottom,
  165. right: padding.right
  166. ),
  167. animated: true
  168. )
  169. }*/
  170. }
  171. .store(in: &cancellables)
  172. //移动至多个点的位置,并提供设置padding距离
  173. viewModel.$suitableLocation
  174. .receive(on: DispatchQueue.main)
  175. .sink{ [weak self] polylines in
  176. if let padding = polylines.last?.mapPadding {
  177. if let atMapPoint : [CLLocationCoordinate2D] = polylines.last?.points {
  178. self?.autonaviMap.moveToSuitableLocation(points: atMapPoint, padding: padding)
  179. //self?.mapView.moveToSuitableLocation(points: atMapPoint, padding: padding)
  180. }
  181. }
  182. }
  183. .store(in: &cancellables)
  184. /*viewModel.$polylines
  185. .receive(on: DispatchQueue.main)
  186. .sink { [weak self] polylines in
  187. guard let self else { return }
  188. // 清除旧覆盖物
  189. self.autonaviMap.removeOverlays(self.autonaviMap.overlays)
  190. // 空数据检查
  191. guard !polylines.isEmpty else {
  192. print("轨迹数据为空")
  193. return
  194. }
  195. // 处理每条轨迹
  196. var validPolylines: [MAPolyline] = []
  197. for polyline in polylines {
  198. // 坐标数量检查
  199. guard !polyline.points.isEmpty else {
  200. print("发现空坐标的轨迹: \(polyline.id)")
  201. continue
  202. }
  203. // 指针转换
  204. let creationResult = polyline.points.withUnsafeBufferPointer { bufferPointer -> MAPolyline? in
  205. guard let baseAddress = bufferPointer.baseAddress, bufferPointer.count > 0 else {
  206. return nil
  207. }
  208. let unsafePointer = UnsafeMutablePointer(mutating: baseAddress)
  209. return MAPolyline(
  210. coordinates: unsafePointer,
  211. count: UInt(bufferPointer.count)
  212. )
  213. }
  214. // 结果检查
  215. guard let mapPolyline = creationResult else {
  216. print("轨迹创建失败: \(polyline.id)")
  217. continue
  218. }
  219. // 绑定属性
  220. mapPolyline.associatedData = [
  221. "id": polyline.id,
  222. "color": polyline.color,
  223. "width": polyline.width
  224. ]
  225. validPolylines.append(mapPolyline)
  226. }
  227. // 添加有效轨迹
  228. guard !validPolylines.isEmpty else {
  229. print("没有有效的轨迹数据")
  230. return
  231. }
  232. self.autonaviMap.addOverlays(validPolylines)
  233. // 调整视野
  234. if let lastValid = validPolylines.last,
  235. let padding = polylines.last?.mapPadding {
  236. self.autonaviMap.showOverlays(
  237. validPolylines,
  238. edgePadding: UIEdgeInsets(
  239. top: padding.top,
  240. left: padding.left,
  241. bottom: padding.bottom,
  242. right: padding.right
  243. ),
  244. animated: true
  245. )
  246. }
  247. }
  248. .store(in: &cancellables)
  249. viewModel.$markers
  250. .receive(on: DispatchQueue.main)
  251. .sink { [weak self] markers in
  252. self?.mapView.removeAnnotations(self?.mapView.annotations ?? [])
  253. UIView.animate(withDuration: 0.3) {
  254. self?.mapView.addAnnotations(markers)
  255. }
  256. }
  257. .store(in: &cancellables)
  258. viewModel.$polylines
  259. .receive(on: DispatchQueue.main)
  260. .sink { [weak self] polylines in
  261. self?.mapView.removeOverlays(self?.mapView.overlays ?? [])
  262. var polyinesList = [MKPolyline]();
  263. for polyline in polylines {
  264. let polylineItem : MKPolyline = polyline.polyline
  265. polylineItem.associatedLineId = polyline.id
  266. polylineItem.associatedLineType = polyline.lineType
  267. polylineItem.associatedColor = polyline.color
  268. polylineItem.associatedWidth = polyline.width
  269. polyinesList.append(polylineItem)
  270. }
  271. self?.mapView.addOverlays(polyinesList)
  272. //self?.mapView.addOverlays(polylines.map({ $0.polyline }))
  273. if let padding = polylines.last?.mapPadding {
  274. self?.mapView.fitsAllPoints(
  275. points: polylines.last?.points.compactMap({ MKMapPoint($0) }) ?? [],
  276. padding: padding.padding,
  277. animated: true
  278. )
  279. }
  280. }
  281. .store(in: &cancellables)
  282. //移动至多个点的位置,并提供设置padding距离
  283. viewModel.$suitableLocation
  284. .receive(on: DispatchQueue.main)
  285. .sink{ [weak self] polylines in
  286. if let padding = polylines.last?.mapPadding {
  287. if let atMapPoint : [CLLocationCoordinate2D] = polylines.last?.points {
  288. self?.mapView.moveToSuitableLocation(points: atMapPoint, padding: padding)
  289. }
  290. }
  291. }
  292. .store(in: &cancellables)*/
  293. }
  294. /*private func bindViewModel() {
  295. viewModel.$markers
  296. .receive(on: DispatchQueue.main)
  297. .sink { [weak self] markers in
  298. self?.autonaviMap.removeAnnotations(self?.autonaviMap.annotations)
  299. var mapMarkerList = [MAPointAnnotation]();
  300. for markerItem in markers {
  301. let annotationItem : MAPointAnnotation = MAPointAnnotation()
  302. annotationItem.title = markerItem.markerName;
  303. annotationItem.coordinate = markerItem.coordinate;
  304. mapMarkerList.append(annotationItem)
  305. }
  306. self?.autonaviMap.addAnnotations(mapMarkerList)
  307. self?.autonaviMap.selectAnnotation(mapMarkerList.first, animated: true)
  308. self?.autonaviMap.setZoomLevel(15.1, animated: true)
  309. let selectIemt : MAPointAnnotation = mapMarkerList.first!
  310. self?.autonaviMap.setCenter(selectIemt.coordinate, animated: true)
  311. //self?.mapView.removeAnnotations(self?.mapView.annotations ?? [])
  312. // UIView.animate(withDuration: 0.3) {
  313. // self?.mapView.addAnnotations(markers)
  314. // }
  315. }
  316. .store(in: &cancellables)
  317. viewModel.$currentRegion
  318. .compactMap { $0 }
  319. .receive(on: DispatchQueue.main)
  320. .sink { [weak self] region in
  321. self?.mapView.setRegion(region, animated: true)
  322. }
  323. .store(in: &cancellables)
  324. viewModel.$markers
  325. .receive(on: DispatchQueue.main)
  326. .sink { [weak self] markers in
  327. self?.mapView.removeAnnotations(self?.mapView.annotations ?? [])
  328. UIView.animate(withDuration: 0.3) {
  329. self?.mapView.addAnnotations(markers)
  330. }
  331. }
  332. .store(in: &cancellables)
  333. viewModel.$polylines
  334. .receive(on: DispatchQueue.main)
  335. .sink { [weak self] polylines in
  336. self?.mapView.removeOverlays(self?.mapView.overlays ?? [])
  337. var polyinesList = [MKPolyline]();
  338. for polyline in polylines {
  339. let polylineItem : MKPolyline = polyline.polyline
  340. polylineItem.associatedLineId = polyline.id
  341. polylineItem.associatedLineType = polyline.lineType
  342. polylineItem.associatedColor = polyline.color
  343. polylineItem.associatedWidth = polyline.width
  344. polyinesList.append(polylineItem)
  345. }
  346. self?.mapView.addOverlays(polyinesList)
  347. //self?.mapView.addOverlays(polylines.map({ $0.polyline }))
  348. if let padding = polylines.last?.mapPadding {
  349. self?.mapView.fitsAllPoints(
  350. points: polylines.last?.points.compactMap({ MKMapPoint($0) }) ?? [],
  351. padding: padding.padding,
  352. animated: true
  353. )
  354. }
  355. }
  356. .store(in: &cancellables)
  357. //移动至多个点的位置,并提供设置padding距离
  358. viewModel.$suitableLocation
  359. .receive(on: DispatchQueue.main)
  360. .sink{ [weak self] polylines in
  361. if let padding = polylines.last?.mapPadding {
  362. if let atMapPoint : [CLLocationCoordinate2D] = polylines.last?.points {
  363. self?.mapView.moveToSuitableLocation(points: atMapPoint, padding: padding)
  364. }
  365. }
  366. }
  367. .store(in: &cancellables)
  368. }*/
  369. }
  370. // 自定义Polyline包装类
  371. private class CustomPolyline {
  372. let polyline: MAPolyline
  373. let id: String
  374. let color: String
  375. let width: Double
  376. init(polyline: MAPolyline, id: String, color: String, width: Double) {
  377. self.polyline = polyline
  378. self.id = id
  379. self.color = color
  380. self.width = width
  381. }
  382. }
  383. private class MyMAPointAnnotation : MAPointAnnotation {
  384. let itemMarker : ATMapMarker
  385. init(itemMarker: ATMapMarker) {
  386. self.itemMarker = itemMarker
  387. }
  388. }
  389. private class MyMAPolyline : MAPolyline {
  390. var itemPolyLine : ATMapPolyline! = nil
  391. }
  392. @available(iOS 13.0, *)
  393. extension MapAmapThemeControl : MAMapViewDelegate {
  394. //根据anntation生成对应的View。
  395. func mapView(_ mapView: MAMapView!, viewFor annotation: MAAnnotation!) -> MAAnnotationView! {
  396. // guard let marker = annotation as? ATMapMarker else {
  397. // return nil
  398. // }
  399. if let myitemAnnotation = annotation as? MyMAPointAnnotation {
  400. if myitemAnnotation.itemMarker.markerType.isMapAchorPoint {
  401. let annotationView = (mapView.dequeueReusableAnnotationView(withIdentifier: MapAnnotationAnchorPointView.identifier) as? MapAnnotationAnchorPointView) ?? MapAnnotationAnchorPointView(annotation: (myitemAnnotation as MAAnnotation), reuseIdentifier: MapAnnotationAnchorPointView.identifier)
  402. annotationView!.marker = myitemAnnotation.itemMarker
  403. return annotationView
  404. } else if (myitemAnnotation.itemMarker.markerType.isTracePopup) {
  405. let annotationBubbleView = (mapView.dequeueReusableAnnotationView(withIdentifier: MapAmapPopUpWindowView.identifier) as? MapAmapPopUpWindowView) ?? MapAmapPopUpWindowView(annotation: (myitemAnnotation as MAAnnotation), reuseIdentifier: MapAmapPopUpWindowView.identifier)
  406. annotationBubbleView!.marker = myitemAnnotation.itemMarker
  407. return annotationBubbleView
  408. }
  409. let annotationView = (mapView.dequeueReusableAnnotationView(withIdentifier: MapAnnotationView.identifier) as? MapAnnotationView) ?? MapAnnotationView(annotation: (myitemAnnotation as MAAnnotation), reuseIdentifier: MapAnnotationView.identifier)
  410. annotationView!.marker = myitemAnnotation.itemMarker
  411. return annotationView
  412. }
  413. return nil
  414. /*if annotation is MyMAPointAnnotation,let myitemAnnotation = annotation as? MyMAPointAnnotation {
  415. let pointReuseIdentifier = "pointReuseIndetifier"
  416. var annotationView = mapView.dequeueReusableAnnotationView(
  417. withIdentifier: pointReuseIdentifier
  418. ) as? MAPinAnnotationView
  419. if annotationView == nil {
  420. annotationView = MAPinAnnotationView(
  421. annotation: annotation,
  422. reuseIdentifier: pointReuseIdentifier
  423. )
  424. }
  425. annotationView?.canShowCallout = true
  426. annotationView?.animatesDrop = true
  427. annotationView?.isDraggable = false
  428. annotationView?.pinColor = .purple
  429. return annotationView
  430. }
  431. return nil*/
  432. }
  433. func mapView(_ mapView: MAMapView!, rendererFor overlay: MAOverlay!) -> MAOverlayRenderer! {
  434. guard let polyline = overlay as? MyMAPolyline else {
  435. return MAPolylineRenderer(polyline: overlay as? MAPolyline)
  436. }
  437. // 外层边框(粗线)
  438. // let borderRenderer = MAPolylineRenderer(polyline: polyline)
  439. // borderRenderer?.lineWidth = 8
  440. // borderRenderer?.strokeColor = UIColor(red: 0.35, green: 0.36, blue: 0.99, alpha: 1)
  441. //
  442. let renderer = MAPolylineRenderer(polyline: polyline)
  443. renderer?.lineWidth = 6
  444. //rgba(123, 125, 255, 1)
  445. renderer?.fillColor = 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)
  446. renderer?.lineCapType = kMALineCapRound//kMALineCapArrow
  447. renderer?.lineJoinType = kMALineJoinRound
  448. // renderer?.strokeColor = UIColor(red: 0.35, green: 0.36, blue: 0.99, alpha: 1)
  449. // renderer?.miterLimit = 1
  450. renderer?.sideColor = UIColor(red: 0.35, green: 0.36, blue: 0.99, alpha: 1)
  451. renderer?.is3DArrowLine = true
  452. // 设置箭头样式
  453. // 加载箭头纹理图片
  454. renderer?.strokeImage = readImageContentFrom(imageName: "com.shishi.dingwei_uparrow")
  455. //normal error selected color
  456. if polyline.itemPolyLine.lineType == "normal" {
  457. renderer?.strokeColor = UIColor(red: 0.27, green: 0.46, blue: 1, alpha: 1)
  458. } else if polyline.itemPolyLine.lineType == "error" {
  459. renderer?.strokeColor = UIColor(red: 1, green: 0.43, blue: 0.43, alpha: 1)
  460. } else if polyline.itemPolyLine.lineType == "selected" {
  461. renderer?.strokeColor = UIColor(red: 0.08, green: 0.8, blue: 0.63, alpha: 1)
  462. } else if polyline.itemPolyLine.lineType == "color" {
  463. renderer?.strokeColor = UIColor(hex: polyline.itemPolyLine.color)
  464. renderer?.lineWidth = polyline.itemPolyLine.width
  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. }*/