Map Zoom Not updated

Trying to navigate between screens and keep the zoom working. When I make the first connection to the map it zooms in just fine. However when I go back to the navigation menu and select map view again it is not zoomed. BTW the zoom unpredictably works sometimes.Thank you

Thanks for the screenshot, but this is hard to diagnose without any details. Are you sure that this is specifically related to Swift? If this issue is reproducible in Objective-C projects too, it will be marked as off-topic on this forum I'm afraid. Additionally, if this is actually Swift-related, you'll need to provide details on the tools and OS versions and ideally isolated code snippets or a link to your GitHub project to reproduce this.

Thank you
This is the code for zooming in which works just fine on the first run but not subsequent views until it is rerun from start.

func mapView(_ myMapView: MKMapView, didAdd views: [MKAnnotationView]) {
// myMapView.setCenter(location?.coordinate, animated: true)
if let annotationView = views.first {
if let annotation = annotationView.annotation {
if annotation is MKUserLocation {
let region = MKCoordinateRegion(center: annotation.coordinate, latitudinalMeters: 4000, longitudinalMeters: 4000)
myMapView.setRegion(region, animated: true)
}
}
}
}

Also here is the code for placing my various annotations on the maps:

import Foundation
import UIKit
import MapKit
import SwiftUI

struct MyMapView: UIViewRepresentable {
@FetchRequest(entity: UserLocation.entity(), sortDescriptors:[NSSortDescriptor(keyPath: \UserLocation.date, ascending: false), NSSortDescriptor(keyPath: \UserLocation.time, ascending: false)]) var userLocations: FetchedResults
@ObservedObject private var locationManager = LocationManager()
@Environment(.managedObjectContext) var moc
@State var showingMap = false
@State var myValue: Int = 0
@State var lat = 49.2057
@State var long = -122.9110
@State var total = 0

    func makeUIView(context: Context) -> MKMapView {
      //MKMapView(frame: .zero)
        let myMapView = MKMapView()
        myMapView.delegate = context.coordinator
        myMapView.showsUserLocation = true
        
        var myCount = userLocations.count
        
        for userLocation in userLocations {
            if (userLocation.date! >= WeeklySpan(increment: myValue)){
                let annotations = MKPointAnnotation()
                annotations.coordinate = CLLocationCoordinate2D(latitude: userLocation.latitude, longitude: userLocation.longitude)
                annotations.title = userLocation.detail
                myMapView.addAnnotations([annotations])
                myCount -= 1
                print(myValue)
                print(myCount)
                print(userLocation.date! as Any)
                print(WeeklySpan(increment: myValue))
            }
            
        }
        return myMapView
    }

Hi @flmcl, I'd like to ask you to not create all sorts of different tags for the threads you're creating. I always need to clean them up on your posts recently. Thank you for your participating. ;)

1 Like
Terms of Service

Privacy Policy

Cookie Policy