MapAmapThemeControl.swift 27 KB

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