MapAmapThemeControl.swift 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. AMapServices.shared().enableHTTPS = true
  29. loadMainMapView()
  30. bindViewModel()
  31. }
  32. private func loadMainMapView() {
  33. autonaviMap = MAMapView()
  34. autonaviMap.delegate = self
  35. autonaviMap.mapType = .standard
  36. autonaviMap.showsUserLocation = false
  37. autonaviMap.userTrackingMode = .follow
  38. autonaviMap.translatesAutoresizingMaskIntoConstraints = false
  39. view.addSubview(autonaviMap)
  40. NSLayoutConstraint.activate([
  41. autonaviMap.topAnchor.constraint(equalTo: view.topAnchor),
  42. autonaviMap.bottomAnchor.constraint(equalTo: view.bottomAnchor),
  43. autonaviMap.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  44. autonaviMap.trailingAnchor.constraint(equalTo: view.trailingAnchor)
  45. ])
  46. }
  47. private func bindViewModel() {
  48. viewModel.$markers
  49. .receive(on: DispatchQueue.main)
  50. .sink { [weak self] markers in
  51. self?.autonaviMap.removeAnnotations(self?.autonaviMap.annotations)
  52. var mapMarkerList = [MAPointAnnotation]();
  53. for markerItem in markers {
  54. let annotationItem : MAPointAnnotation = MAPointAnnotation()
  55. annotationItem.title = markerItem.markerName;
  56. annotationItem.coordinate = markerItem.coordinate;
  57. mapMarkerList.append(annotationItem)
  58. }
  59. self?.autonaviMap.addAnnotations(mapMarkerList)
  60. self?.autonaviMap.selectAnnotation(mapMarkerList.first, animated: true)
  61. self?.autonaviMap.setZoomLevel(15.1, animated: true)
  62. let selectIemt : MAPointAnnotation = mapMarkerList.first!
  63. self?.autonaviMap.setCenter(selectIemt.coordinate, animated: true)
  64. //self?.mapView.removeAnnotations(self?.mapView.annotations ?? [])
  65. // UIView.animate(withDuration: 0.3) {
  66. // self?.mapView.addAnnotations(markers)
  67. // }
  68. }
  69. .store(in: &cancellables)
  70. /*viewModel.$currentRegion
  71. .compactMap { $0 }
  72. .receive(on: DispatchQueue.main)
  73. .sink { [weak self] region in
  74. self?.mapView.setRegion(region, animated: true)
  75. }
  76. .store(in: &cancellables)
  77. viewModel.$markers
  78. .receive(on: DispatchQueue.main)
  79. .sink { [weak self] markers in
  80. self?.mapView.removeAnnotations(self?.mapView.annotations ?? [])
  81. UIView.animate(withDuration: 0.3) {
  82. self?.mapView.addAnnotations(markers)
  83. }
  84. }
  85. .store(in: &cancellables)
  86. viewModel.$polylines
  87. .receive(on: DispatchQueue.main)
  88. .sink { [weak self] polylines in
  89. self?.mapView.removeOverlays(self?.mapView.overlays ?? [])
  90. var polyinesList = [MKPolyline]();
  91. for polyline in polylines {
  92. let polylineItem : MKPolyline = polyline.polyline
  93. polylineItem.associatedLineId = polyline.id
  94. polylineItem.associatedLineType = polyline.lineType
  95. polylineItem.associatedColor = polyline.color
  96. polylineItem.associatedWidth = polyline.width
  97. polyinesList.append(polylineItem)
  98. }
  99. self?.mapView.addOverlays(polyinesList)
  100. //self?.mapView.addOverlays(polylines.map({ $0.polyline }))
  101. if let padding = polylines.last?.mapPadding {
  102. self?.mapView.fitsAllPoints(
  103. points: polylines.last?.points.compactMap({ MKMapPoint($0) }) ?? [],
  104. padding: padding.padding,
  105. animated: true
  106. )
  107. }
  108. }
  109. .store(in: &cancellables)
  110. //移动至多个点的位置,并提供设置padding距离
  111. viewModel.$suitableLocation
  112. .receive(on: DispatchQueue.main)
  113. .sink{ [weak self] polylines in
  114. if let padding = polylines.last?.mapPadding {
  115. if let atMapPoint : [CLLocationCoordinate2D] = polylines.last?.points {
  116. self?.mapView.moveToSuitableLocation(points: atMapPoint, padding: padding)
  117. }
  118. }
  119. }
  120. .store(in: &cancellables)*/
  121. }
  122. }
  123. @available(iOS 13.0, *)
  124. extension MapAmapThemeControl : MAMapViewDelegate {
  125. //根据anntation生成对应的View。
  126. func mapView(_ mapView: MAMapView!, viewFor annotation: MAAnnotation!) -> MAAnnotationView! {
  127. if annotation is MAPointAnnotation {
  128. let pointReuseIdentifier = "pointReuseIndetifier"
  129. var annotationView = mapView.dequeueReusableAnnotationView(
  130. withIdentifier: pointReuseIdentifier
  131. ) as? MAPinAnnotationView
  132. if annotationView == nil {
  133. annotationView = MAPinAnnotationView(
  134. annotation: annotation,
  135. reuseIdentifier: pointReuseIdentifier
  136. )
  137. }
  138. annotationView?.canShowCallout = true
  139. annotationView?.animatesDrop = true
  140. annotationView?.isDraggable = false
  141. annotationView?.pinColor = .purple
  142. return annotationView
  143. }
  144. return nil
  145. }
  146. }
  147. /*
  148. @available(iOS 13.0, *)
  149. extension MapAmapThemeControl: MKMapViewDelegate {
  150. func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
  151. guard let marker = annotation as? ATMapMarker else {
  152. return nil
  153. }
  154. print("markerTypesfdsdfs---\(marker.markerType.markType)");
  155. if marker.markerType.isMapAchorPoint {
  156. let annotationView = (mapView.dequeueReusableAnnotationView(withIdentifier: MapAnnotationAnchorPointView.identifier) as? MapAnnotationAnchorPointView) ?? MapAnnotationAnchorPointView(annotation: marker, reuseIdentifier: MapAnnotationAnchorPointView.identifier)
  157. annotationView.marker = marker
  158. annotationView.prepareForDisplay()
  159. return annotationView
  160. } else if (marker.markerType.isTracePopup) {
  161. let annotationBubbleView = (mapView.dequeueReusableAnnotationView(withIdentifier: MapAmapPopUpWindowView.identifier) as? MapAmapPopUpWindowView) ?? MapAmapPopUpWindowView(annotation: marker, reuseIdentifier: MapAmapPopUpWindowView.identifier)
  162. annotationBubbleView.marker = marker
  163. annotationBubbleView.prepareForDisplay()
  164. return annotationBubbleView
  165. }
  166. let annotationView = (mapView.dequeueReusableAnnotationView(withIdentifier: MapAnnotationView.identifier) as? MapAnnotationView) ?? MapAnnotationView(annotation: marker, reuseIdentifier: MapAnnotationView.identifier)
  167. annotationView.marker = marker
  168. annotationView.prepareForDisplay()
  169. return annotationView
  170. }
  171. func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
  172. guard var marker = view.annotation as? ATMapMarker else {
  173. return
  174. }
  175. viewModel.handleMarkerTap(marker: &marker)
  176. }
  177. func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
  178. guard let polyline = overlay as? MKPolyline else {
  179. return MKOverlayRenderer(overlay: overlay)
  180. }
  181. let renderer = MapAmapArrowPolylineRenderer(polyline: polyline)
  182. renderer.lineWidth = 6
  183. //rgba(123, 125, 255, 1)
  184. 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)
  185. renderer.lineCap = .round
  186. renderer.lineJoin = .round
  187. // 设置箭头样式
  188. renderer.arrowColor = UIColor.white // 箭头颜色
  189. renderer.arrowSize = 12 // 增大箭头大小
  190. renderer.arrowSpacing = 50 // 减小箭头间距
  191. renderer.borderWidth = 1
  192. renderer.borderColor = UIColor(red: 0.35, green: 0.36, blue: 0.99, alpha: 1)
  193. //normal error selected color
  194. if polyline.associatedLineType == "normal" {
  195. renderer.strokeColor = UIColor(red: 0.27, green: 0.46, blue: 1, alpha: 1)
  196. } else if polyline.associatedLineType == "error" {
  197. renderer.strokeColor = UIColor(red: 1, green: 0.43, blue: 0.43, alpha: 1)
  198. } else if polyline.associatedLineType == "selected" {
  199. renderer.strokeColor = UIColor(red: 0.08, green: 0.8, blue: 0.63, alpha: 1)
  200. } else if polyline.associatedLineType == "color" {
  201. renderer.strokeColor = UIColor(hex: polyline.associatedColor ?? "#4476FF")
  202. renderer.lineWidth = polyline.associatedWidth ?? 0
  203. }
  204. return renderer
  205. }
  206. }*/