0

I am trying to zoom out of a map but it keeps zooming back in because of the .userTrackingMode that is set to .follow. Is there a way to prevent or to write your own tracking mode method?

I tried to disable the tracking for 20seconds or so, but I ended up with the same problem. I also tried to just use the camera by sending it of to the users location but that did not work either. Here is my code:

import SwiftUI
import Combine
import MapKit

struct MyMapView: UIViewRepresentable {
    @State var locationManager = CLLocationManager()
    @Binding var span: Double
    
    class Coordinator: NSObject, MKMapViewDelegate {
        var parent: MyMapView
        
        init(_ parent: MyMapView) {
            self.parent = parent
            super.init()
        }
    }
    
    func makeUIView(context: Context) -> MKMapView {
        setupManager()
        
        let mapView = MKMapView(frame: UIScreen.main.bounds)
        mapView.mapType = MKMapType.satellite
        mapView.showsUserLocation = true
        mapView.showsCompass = false
        mapView.isZoomEnabled = true
        
        mapView.delegate = context.coordinator
        
        // Version 1
        mapView.userTrackingMode = .follow
        
        /*
        // Version 2
        let camera1 = MKMapCamera(
            lookingAtCenter: mapView.centerCoordinate,
            fromEyeCoordinate: mapView.centerCoordinate,
            eyeAltitude: span)
        mapView.setCamera(camera1, animated: false)
        */
         
        return mapView
    }
    
    func updateUIView(_ uiView: MKMapView, context: Context) {
        
        // Version 1 -- zoom out of the map over the span variable
        uiView.setRegion(MKCoordinateRegion(center: uiView.centerCoordinate, latitudinalMeters: CLLocationDistance(span), longitudinalMeters: CLLocationDistance(span)), animated: false)
        
        // Deactivating the tracking and activating the tracking after 60sec and then disable it again.
        uiView.userTrackingMode = .none
        DispatchQueue.main.asyncAfter(deadline: .now() + 60.0) {
            uiView.userTrackingMode = .follow
        }
        uiView.userTrackingMode = .none

        /*
        // Version 2 -- Camera
        let camera2 = MKMapCamera(
            lookingAtCenter: uiView.centerCoordinate,
            fromEyeCoordinate: uiView.centerCoordinate,
            eyeAltitude: span)
        uiView.setCamera(camera2, animated: false)
        */
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    func setupManager() {
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.requestAlwaysAuthorization()
    }
}

Is there a solution for my problem?

C Fuchs
  • 1
  • 1

0 Answers0