MapViewController.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // MapViewController.swift
  3. // Runner
  4. //
  5. // Created by Groot on 2025/5/7.
  6. //
  7. import UIKit
  8. import MapKit
  9. import Combine
  10. class MapViewController: UIViewController {
  11. required init?(coder: NSCoder) {
  12. fatalError("init(coder:) has not been implemented")
  13. }
  14. let viewModel: MapViewModel
  15. let mapView = MKMapView()
  16. private var cancellables = Set<AnyCancellable>()
  17. init(viewModel: MapViewModel) {
  18. self.viewModel = viewModel
  19. super.init(nibName: nil, bundle: nil)
  20. }
  21. override func viewDidLoad() {
  22. super.viewDidLoad()
  23. setupView()
  24. setupMap()
  25. bindViewModel()
  26. }
  27. private func setupView() {
  28. mapView.translatesAutoresizingMaskIntoConstraints = false
  29. view.addSubview(mapView)
  30. NSLayoutConstraint.activate([
  31. mapView.topAnchor.constraint(equalTo: view.topAnchor),
  32. mapView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
  33. mapView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  34. mapView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
  35. ])
  36. }
  37. private func setupMap() {
  38. mapView.delegate = self
  39. mapView.showsUserLocation = true
  40. mapView.setUserTrackingMode(.follow, animated: true)
  41. }
  42. private func bindViewModel() {
  43. // 监听区域变化并更新地图
  44. viewModel.$currentRegion
  45. .compactMap { $0 }
  46. .receive(on: DispatchQueue.main)
  47. .sink { [weak self] region in
  48. self?.mapView.setRegion(region, animated: true)
  49. }
  50. .store(in: &cancellables)
  51. viewModel.$markers
  52. .receive(on: DispatchQueue.main)
  53. .sink { [weak self] markers in
  54. self?.mapView.removeAnnotations(self?.mapView.annotations ?? [])
  55. self?.mapView.addAnnotations(markers)
  56. }
  57. .store(in: &cancellables)
  58. viewModel.$polylines
  59. .receive(on: DispatchQueue.main)
  60. .sink { [weak self] polylines in
  61. self?.mapView.removeOverlays(self?.mapView.overlays ?? [])
  62. self?.mapView.addOverlays(polylines.map({ $0.polyline }))
  63. }
  64. .store(in: &cancellables)
  65. }
  66. }
  67. extension MapViewController: MKMapViewDelegate {
  68. func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
  69. guard let marker = annotation as? ATMapMarker else {
  70. return nil
  71. }
  72. let annotationView = (mapView.dequeueReusableAnnotationView(withIdentifier: marker.markerType.imageName(selected: marker.isSelected)) as? MapAnnotationView) ?? MapAnnotationView(annotation: marker, reuseIdentifier: marker.markerType.imageName(selected: marker.isSelected))
  73. annotationView.marker = marker
  74. annotationView.prepareForDisplay()
  75. annotationView.prepareForReuse()
  76. return annotationView
  77. }
  78. func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
  79. guard var marker = view.annotation as? ATMapMarker else {
  80. return
  81. }
  82. viewModel.handleMarkerTap(marker: &marker)
  83. }
  84. func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
  85. let renderer = MKPolylineRenderer(overlay: overlay)
  86. renderer.lineWidth = 6
  87. //rgba(123, 125, 255, 1)
  88. renderer.strokeColor = UIColor(red: 123 / 255, green: 125 / 255, blue: 255 / 255, alpha: 1)
  89. renderer.lineCap = .round
  90. renderer.lineJoin = .round
  91. return renderer
  92. }
  93. }