feat: 支持模拟器定位与地图 fallback
This commit is contained in:
@ -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
|
||||
|
||||
Reference in New Issue
Block a user