steve_sch
steve_sch

Reputation: 656

MapKit .mapStyle pointsOfInterest with selectable .mapStyle

I used an example found here to enable user selectable mapStyle:

    private var selectedMapStyle: MapStyle {
        return switch(mapType) {
        case 0: .standard
        case 1: .hybrid
        case 2: .imagery
        default: .standard
        }
    }
    @State private var mapType: Int = 0

This works great for adding a Button to toggle between hybrid and standard mapStyles. But adding a pointsOfInterest filter does not.

This works:

.mapStyle(selectedMapStyle)

and this works:

.mapStyle(.standard(pointsOfInterest: .excludingAll))

but this doesn't:

.mapStyle(selectedMapStyle(pointsOfInterest: .excludingAll))

The errors are: "Cannot call value of non-function type 'MapStyle'" and "Cannot infer contextual base in reference to member 'excludingAll'"

How do I make the compiler happy for this combo? Thanks!

Added: example code:

import SwiftUI
import MapKit

struct ContentView: View {
    
    private var selectedMapStyle: MapStyle {
        return switch(mapType) {
        case 0: .standard
        case 1: .hybrid
        case 2: .imagery
        default: .standard
        }
    }
    @State private var mapType: Int = 0

    var body: some View {
        Map ()
        // this works:
        .mapStyle(selectedMapStyle)
        // this works:
        //.mapStyle(.hybrid(pointsOfInterest: .excludingAll))
        //this doesn't work:
        //.mapStyle(selectedMapStyle(pointsOfInterest: .excludingAll))
        Button(action: {
            if mapType == 0 {
                mapType = 1
            } else {
                mapType = 0
            }
        })
        {
            VStack {
                Image(systemName: "map")
                    .font(.system(size: 30))
                Text("Map Style")
                    .font(.subheadline)
            }
        }
    }
}

#Preview {
    ContentView()
}

Upvotes: 0

Views: 105

Answers (1)

Kushagra
Kushagra

Reputation: 986

Your variable selectedMapStyle is a type of MapStyle. However, .hybrid(pointsOfInterest: .excludingAll) is a function which returns a MapStyle with supplied parameters. That's the reason the compiler is throwing an error.

So adding the appropriate function in your switch case instead of the enum works. You can change the selectedPointsOfInterest to update the map style.

Here's the code:

struct ContentView: View {
    private var selectedMapStyle: MapStyle {
        return switch(mapType) {
        case 0: .standard(pointsOfInterest: selectedPointsOfInterest)
        case 1: .hybrid(pointsOfInterest: selectedPointsOfInterest)
        case 2: .imagery
        default: .standard
        }
    }
    
    @State private var mapType: Int = 0
    @State private var selectedPointsOfInterest: PointOfInterestCategories = .excludingAll

    var body: some View {
        Map ()
        .mapStyle(selectedMapStyle)
        Button(action: {
            if mapType == 0 {
                mapType = 1
            } else {
                mapType = 0
            }
        }) {
            VStack {
                Image(systemName: "map")
                    .font(.system(size: 30))
                Text("Map Style")
                    .font(.subheadline)
            }
        }
    }
}

Upvotes: 1

Related Questions