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

Keep losing mapType and pointOfInterestFilter when coordinateRegion updates

Forums > SwiftUI

I'm building an app entirely in SwiftUI as part of a University research project. I'd like to use the .mutedStandard map type and to remove all but a few of Apple's points of interest (POI) as they can become quite cluttered. My approach works perfectly fine when the map is first loaded, but things change as soon as the mapRegion binding changes in the parent view.

The region and annotations update exactly as desired when the binding of the mapRegion changes, but from the first change the map begins to ignore the .mapType and .pointOfInterstFilter created in the map's .onAppear code, thus reverting back to Apple's default, POI-littered map.

I've played around with moving these to the view's init(), attempting reloads using .id() all to no avail. If anyone has some suggestions, I'd be very greatful!

Here's the view code:

struct LocationPickerMap: View {
    @Binding var mapRegion: MKCoordinateRegion
    var annotations: [Markers]

    let mapFilters: MKPointOfInterestFilter = MKPointOfInterestFilter(including: [.airport, .beach, .gasStation, .hospital, .library, .park, .police, .postOffice, .school, .university, .zoo])

    var body: some View {
        VStack {
            Map(
                coordinateRegion: $mapRegion,
                interactionModes: [],
                showsUserLocation: true,
                annotationItems: annotations
            ) { item in
                MapMarker(
                    coordinate: CLLocationCoordinate2D(
                        latitude: item.coordinate.latitude,
                        longitude: item.coordinate.longitude
                    ),
                    tint: Color.red
                )
            }
        }
        .onAppear {
            MKMapView.appearance().mapType = .mutedStandard
            MKMapView.appearance().pointOfInterestFilter = .some(mapFilters)
        }
    }
}

Ryan

2      

Just guessing here. Perhaps it is because Map has not all features a MKMapView has. You could try and replace Map with MKMapView in a UIViewRepresentable and give it a try.

2      

@Hatsushira, yeah that's my fallback if I can't find a pure Map solution, but I'm still holding out hope. :)

2      

Have you tried to set these on app start instead of in the view itself? Perhaps the behaviour is intended when changing the region and you have to set it again?

2      

@Hatsushira, I gave it a shot, but same thing seems to happen. Likely going to have to either accept this behaviour or switch back to MKMapView.

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.