UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

MapKit Map Buggy in NavigationLink

Forums > SwiftUI

TL;DR -- Below I have used Map in two very simple ways in the example below but one is buggy and the other is not. Why?

I've used Map(coordinateRegion:) successfully before but I'm running into an issue now where if I use that map as the destination of a NavigationLink then the scrolling is terrible. If I make a new view (here MyMapView) that has nothing more than a @Binding to the region it suddely works smoothly. What is going on here to cause this issue? I'm not having luck solving this problem in the same way in my bigger app but it seems to be the same problem so if I can understand the root of it here I can hopefully apply it back to the other project. On the simulator the "slow" version often works but on device it is obviously bad every time.

import SwiftUI
import MapKit

struct ContentView: View {
    @State private var region1 = MKCoordinateRegion(.world)
    @State private var region2 = MKCoordinateRegion(.world)

    var body: some View {
        NavigationView {
            List {
                NavigationLink {
                    Map(coordinateRegion: $region1)
                } label: {
                    Text("Slow Map")
                }
                NavigationLink {
                    MyMapView(region: $region2)
                } label: {
                    Text("Good Map")
                }
            }
        }
    }
}

struct MyMapView: View {
    @Binding var region: MKCoordinateRegion

    var body: some View {
        Map(coordinateRegion: $region)
    }
}

2      

I think the reason is that when you pass in the coordinateRegion into Map when you scroll the map it need to update the @State however it does not have access to region1 as it in a new view, however when you use MyMapView it has access to region as it in the same struct

If you look at the "Slow Map" you will see you can not scroll the map at all (not just buggy!). It trys to move it but can not update the coordinates so in snap back to orginal.

Hope that is understandable

2      

I might be missing a nuance in your description but I don't see how this explains the problem in any more detail than what I have shown in the code. The slow map can be moved even though it's not very easy.

2      

I found some related discussion (linked below) that explains it somewhat well but it then raises the question for me of why it does work for @Binding. In any case, what I've done is switched to the UIKit version and then used mapView(_:regionDidChangeAnimated:) and it works almost perfectly so I'm just going to use that for now.

https://stackoverflow.com/questions/67859494/swiftui-how-to-get-rid-of-constant-initialization-of-views-linked-through-naviga

https://stackoverflow.com/questions/60278829/obervableobject-being-init-multiple-time-and-not-refreshing-my-view

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.