MapViewController.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. mapView.showsUserLocation = false
  21. }
  22. override func viewDidLoad() {
  23. super.viewDidLoad()
  24. setupView()
  25. setupMap()
  26. bindViewModel()
  27. }
  28. private func setupView() {
  29. mapView.translatesAutoresizingMaskIntoConstraints = false
  30. view.addSubview(mapView)
  31. NSLayoutConstraint.activate([
  32. mapView.topAnchor.constraint(equalTo: view.topAnchor),
  33. mapView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
  34. mapView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  35. mapView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
  36. ])
  37. }
  38. private func setupMap() {
  39. mapView.delegate = self
  40. mapView.showsUserLocation = false
  41. mapView.mapType = .mutedStandard
  42. }
  43. private func bindViewModel() {
  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. if let padding = polylines.last?.mapPadding {
  64. self?.mapView.fitsAllPoints(
  65. points: polylines.last?.points.compactMap({ MKMapPoint($0) }) ?? [],
  66. padding: padding.padding,
  67. animated: true
  68. )
  69. }
  70. }
  71. .store(in: &cancellables)
  72. }
  73. }
  74. extension MapViewController: MKMapViewDelegate {
  75. func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
  76. guard let marker = annotation as? ATMapMarker else {
  77. return nil
  78. }
  79. let annotationView = (mapView.dequeueReusableAnnotationView(withIdentifier: marker.markerType.imageName(selected: marker.isSelected)) as? MapAnnotationView) ?? MapAnnotationView(annotation: marker, reuseIdentifier: marker.markerType.imageName(selected: marker.isSelected))
  80. annotationView.marker = marker
  81. annotationView.prepareForDisplay()
  82. annotationView.prepareForReuse()
  83. return annotationView
  84. }
  85. func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
  86. guard var marker = view.annotation as? ATMapMarker else {
  87. return
  88. }
  89. viewModel.handleMarkerTap(marker: &marker)
  90. }
  91. func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
  92. let renderer = MKPolylineRenderer(overlay: overlay)
  93. renderer.lineWidth = 6
  94. //rgba(123, 125, 255, 1)
  95. renderer.strokeColor = UIColor(red: 123 / 255, green: 125 / 255, blue: 255 / 255, alpha: 1)
  96. renderer.lineCap = .round
  97. renderer.lineJoin = .round
  98. return renderer
  99. }
  100. }