summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAndrej Mihajlov <and@mullvad.net>2023-01-25 14:05:51 +0100
committerAndrej Mihajlov <and@mullvad.net>2023-01-30 15:09:41 +0100
commit088bedb8208c295c2ddab5b248edc0bf1ce22862 (patch)
tree185fd42239b17b4922f444d7d68d274a46e746f6
parenteb91ae2f3609841def5735c5a837d1bbd3f3fd10 (diff)
downloadmullvadvpn-088bedb8208c295c2ddab5b248edc0bf1ce22862.tar.xz
mullvadvpn-088bedb8208c295c2ddab5b248edc0bf1ce22862.zip
MKMapView: guard against kCLLocationCoordinate2DInvalid
MKMapView.convert(_, toRegionFrom:) may return a region with center set to (-180, -180) when it cannot compute the geo region. Normally this occurs before the first layout pass, i.e when the map view is squashed to zero (0,0,0,0)
-rw-r--r--ios/MullvadVPN/MapViewController.swift29
1 files changed, 19 insertions, 10 deletions
diff --git a/ios/MullvadVPN/MapViewController.swift b/ios/MullvadVPN/MapViewController.swift
index 2ea933d9ef..9a8a434dd3 100644
--- a/ios/MullvadVPN/MapViewController.swift
+++ b/ios/MullvadVPN/MapViewController.swift
@@ -22,7 +22,7 @@ final class MapViewController: UIViewController, MKMapViewDelegate {
private var willChangeRegion = false
private var regionDidChangeCompletion: (() -> Void)?
private let mapView = MKMapView()
-
+ private var isFirstLayoutPass = true
private var center: CLLocationCoordinate2D?
var alignmentView: UIView?
@@ -62,6 +62,15 @@ final class MapViewController: UIViewController, MKMapViewDelegate {
})
}
+ override func viewDidLayoutSubviews() {
+ super.viewDidLayoutSubviews()
+
+ if isFirstLayoutPass {
+ isFirstLayoutPass = false
+ recomputeVisibleRegion(animated: false)
+ }
+ }
+
// MARK: - Public
func addLocationMarker(coordinate: CLLocationCoordinate2D) {
@@ -179,13 +188,6 @@ final class MapViewController: UIViewController, MKMapViewDelegate {
self.center = center
- logger.debug(
- """
- Set map region to: (\(region.center.latitude), \(region.center.longitude), \
- \(region.span.latitudeDelta), \(region.span.longitudeDelta))
- """
- )
-
// Map view does not call delegate methods when attempting to set the same region.
mapView.setRegion(region, animated: animated)
@@ -254,7 +256,10 @@ final class MapViewController: UIViewController, MKMapViewDelegate {
) -> MKCoordinateRegion {
// Map view center lies within layout margins frame.
let mapViewLayoutFrame = mapView.layoutMarginsGuide.layoutFrame
- guard mapViewLayoutFrame.width > 0 else { return region }
+
+ guard mapViewLayoutFrame.width > 0, mapView.frame.width > 0,
+ region.span.longitudeDelta > 0,
+ mapView.region.span.longitudeDelta > 0 else { return region }
// MKMapView.convert(_:toRectTo:) returns CGRect scaled to the zoom level derived from
// currently set region.
@@ -272,6 +277,10 @@ final class MapViewController: UIViewController, MKMapViewDelegate {
let offsetRegionRect = regionRect.offsetBy(dx: horizontalOffset, dy: verticalOffset)
let offsetRegion = mapView.convert(offsetRegionRect, toRegionFrom: mapView)
- return offsetRegion
+ if CLLocationCoordinate2DIsValid(offsetRegion.center) {
+ return offsetRegion
+ } else {
+ return region
+ }
}
}