修复位置上报解析并立即居中到用户位置
This commit is contained in:
@ -294,6 +294,7 @@ final class LocationReportMapView: UIView, MAMapViewDelegate {
|
||||
|
||||
let mapView: MAMapView
|
||||
private var markerAnnotation: MAPointAnnotation?
|
||||
private var shouldCenterOnNextUserLocation = false
|
||||
|
||||
override init(frame: CGRect) {
|
||||
_ = AMapBootstrap.configureIfNeeded()
|
||||
@ -314,9 +315,14 @@ final class LocationReportMapView: UIView, MAMapViewDelegate {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
/// 进入页面后,在首次拿到用户位置时将地图居中(不持续跟随,便于手动拖动)。
|
||||
func enableInitialUserLocationCentering() {
|
||||
shouldCenterOnNextUserLocation = true
|
||||
centerOnUserLocationIfNeeded()
|
||||
}
|
||||
|
||||
func centerOnUserLocation() {
|
||||
let coordinate = mapView.userLocation.location?.coordinate
|
||||
guard let coordinate, CLLocationCoordinate2DIsValid(coordinate) else { return }
|
||||
guard let coordinate = validUserCoordinate else { return }
|
||||
setCenter(coordinate)
|
||||
}
|
||||
|
||||
@ -345,7 +351,25 @@ final class LocationReportMapView: UIView, MAMapViewDelegate {
|
||||
mapView.addAnnotation(annotation)
|
||||
}
|
||||
|
||||
func mapView(_ mapView: MAMapView!, didUpdate userLocation: MAUserLocation!, updatingLocation: Bool) {
|
||||
guard updatingLocation else { return }
|
||||
centerOnUserLocationIfNeeded()
|
||||
}
|
||||
|
||||
func mapView(_ mapView: MAMapView!, didSingleTappedAt coordinate: CLLocationCoordinate2D) {
|
||||
onMapTap?(coordinate)
|
||||
}
|
||||
|
||||
private var validUserCoordinate: CLLocationCoordinate2D? {
|
||||
let coordinate = mapView.userLocation.location?.coordinate
|
||||
guard let coordinate, CLLocationCoordinate2DIsValid(coordinate) else { return nil }
|
||||
guard 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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user