MapViewController.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. .sink { [weak self] region in
  47. self?.mapView.setRegion(region, animated: true)
  48. }
  49. .store(in: &cancellables)
  50. viewModel.$markers
  51. .sink { [weak self] markers in
  52. self?.mapView.removeAnnotations(self?.mapView.annotations ?? [])
  53. self?.mapView.addAnnotations(markers)
  54. }
  55. .store(in: &cancellables)
  56. viewModel.$polylines
  57. .sink { [weak self] polylines in
  58. self?.mapView.removeOverlays(self?.mapView.overlays ?? [])
  59. self?.mapView.addOverlays(polylines.map({ $0.polyline }))
  60. }
  61. .store(in: &cancellables)
  62. }
  63. }
  64. extension MapViewController: MKMapViewDelegate {
  65. func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
  66. var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: MapAnnotationView.identifier) as? MapAnnotationView
  67. // 如果没有可复用的视图,创建新的自定义标注视图
  68. if annotationView == nil {
  69. annotationView = MapAnnotationView(annotation: annotation, reuseIdentifier: MapAnnotationView.identifier)
  70. } else {
  71. // 更新现有视图的标注
  72. annotationView?.annotation = annotation
  73. }
  74. return annotationView
  75. }
  76. func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
  77. guard var marker = view.annotation as? ATMapMarker else {
  78. return
  79. }
  80. viewModel.handleMarkerTap(marker: &marker)
  81. }
  82. func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
  83. let renderer = MKPolylineRenderer(overlay: overlay)
  84. renderer.lineWidth = 5
  85. renderer.strokeColor = .blue
  86. renderer.alpha = 1
  87. renderer.lineCap = .round
  88. renderer.lineJoin = .round
  89. return renderer
  90. }
  91. }