feat: 支持模拟器定位与地图 fallback

This commit is contained in:
2026-07-24 10:04:55 +08:00
parent 0e0415eec4
commit f87b13cc05
24 changed files with 4040 additions and 2161 deletions

View File

@ -7,6 +7,10 @@ import CoreLocation
import SnapKit
import UIKit
#if targetEnvironment(simulator)
import MapKit
#endif
/// /
final class LocationReportMapControlStack: UIView {
@ -295,7 +299,112 @@ final class LocationReportBottomActionView: UIControl {
}
}
/// `MAMapView`
/// 使 MapKit
#if targetEnvironment(simulator)
final class LocationReportMapView: UIView, MKMapViewDelegate {
var onMapTap: ((CLLocationCoordinate2D) -> Void)?
let mapView: MKMapView
private var markerAnnotation: MKPointAnnotation?
private var shouldCenterOnNextUserLocation = false
override init(frame: CGRect) {
mapView = MKMapView(frame: .zero)
super.init(frame: frame)
mapView.delegate = self
mapView.showsUserLocation = true
mapView.showsCompass = false
addSubview(mapView)
mapView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(mapTapped(_:)))
tapGesture.cancelsTouchesInView = false
mapView.addGestureRecognizer(tapGesture)
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
///
func enableInitialUserLocationCentering() {
shouldCenterOnNextUserLocation = true
centerOnUserLocationIfNeeded()
}
func centerOnUserLocation() {
guard let coordinate = validUserCoordinate else { return }
setCenter(coordinate)
}
func setCenter(_ coordinate: CLLocationCoordinate2D, animated: Bool = true) {
let region = MKCoordinateRegion(
center: coordinate,
span: MKCoordinateSpan(latitudeDelta: 0.012, longitudeDelta: 0.012)
)
mapView.setRegion(region, animated: animated)
}
func zoomIn() {
updateZoom(scale: 0.5)
}
func zoomOut() {
updateZoom(scale: 2)
}
func updateMarker(latitude: Double?, longitude: Double?) {
if let markerAnnotation {
mapView.removeAnnotation(markerAnnotation)
self.markerAnnotation = nil
}
guard let latitude, let longitude else { return }
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
markerAnnotation = annotation
mapView.addAnnotation(annotation)
}
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
centerOnUserLocationIfNeeded()
}
@objc private func mapTapped(_ gesture: UITapGestureRecognizer) {
guard gesture.state == .ended else { return }
let point = gesture.location(in: mapView)
onMapTap?(mapView.convert(point, toCoordinateFrom: mapView))
}
private var validUserCoordinate: CLLocationCoordinate2D? {
guard let coordinate = mapView.userLocation.location?.coordinate,
CLLocationCoordinate2DIsValid(coordinate),
coordinate.latitude != 0 || coordinate.longitude != 0 else {
return nil
}
return coordinate
}
private func centerOnUserLocationIfNeeded() {
guard shouldCenterOnNextUserLocation, let coordinate = validUserCoordinate else { return }
setCenter(coordinate, animated: false)
shouldCenterOnNextUserLocation = false
}
private func updateZoom(scale: Double) {
let current = mapView.region
let span = MKCoordinateSpan(
latitudeDelta: min(max(current.span.latitudeDelta * scale, 0.0005), 120),
longitudeDelta: min(max(current.span.longitudeDelta * scale, 0.0005), 120)
)
mapView.setRegion(MKCoordinateRegion(center: current.center, span: span), animated: true)
}
}
#else
/// `MAMapView`
final class LocationReportMapView: UIView, MAMapViewDelegate {
var onMapTap: ((CLLocationCoordinate2D) -> Void)?
@ -381,3 +490,4 @@ final class LocationReportMapView: UIView, MAMapViewDelegate {
shouldCenterOnNextUserLocation = false
}
}
#endif