Can you make a Marker selectable?

In the initial Xcode 15/iOS 17 Beta selecting a Marker would give a visible indication that the marker was selected without setting the selection. This is the code I used.

struct ContentView: View {
    let location = CLLocationCoordinate2D(latitude: 37.0,
                                          longitude: -122.0)
    @State private var position: MapCameraPosition = .automatic
    @State private var selectedItem: MKMapItem?

    var body: some View {
        VStack {
            Text(selectedItem == nil ? "Nothing Selected" :
                    selectedItem?.name == nil ? "No name" :
                    selectedItem!.name!)
                .bold()
                .padding()
            Map(position: $position, selection: $selectedItem) {
                Marker("Marker", coordinate: location)
                    .tag(1)
                    .tint(.red)
            }
        }
        .padding()
    }
}

I submitted feedback and things changed in Beta 3. Now I can not select the Marker. That's not the direction I'd hoped to see. Am I doing something wrong or is there no way to select a Marker placed on a map?

Answered by marchyman in 772958022

Ahh, I'd forgotten I posted this to the forum. After getting a response from Apple on my feedback I changed the code to something like this:

struct ContentView: View {
    // values normally specified by the caller....
    let location = CLLocationCoordinate2D(latitude: 37.0, longitude: -122.0)
    let name = "name"

    @State private var position: MapCameraPosition = .automatic
    @State private var selectedId: String?

    var body: some View {
        let markers = makeMarkers(for: name, location: location)
        Map(position: $position, selection: $selectedId) {
            ForEach(markers) { marker in
                Marker(marker.title, coordinate: marker.location)
                    .tint(.red)
            }
        }
    }

    // Apparently needed for marker selection
    struct MarkerModel: Identifiable {
        var id: String = UUID().uuidString
        var location: CLLocationCoordinate2D
        var title: String
    }

    private func makeMarkers(for text: String, location: CLLocationCoordinate2D) -> [MarkerModel] {
        let marker = MarkerModel(location: location,
                                 title: text)
        return [marker]
    }
}

Code typed into this reply and may well contain typos. Anyway, using MKMapItem did not work. Not shown is the code that checks if a marker is selected where I put up a LookAround view. I do that in an overlay of the Map view.

There seems to still be issues with how selection works, but in your case, once you designate a tag. the selection type needs to be Int to get that tag value, or at least that's what seemed to work for me based on a tutorial I've lost the link to.

Accepted Answer

Ahh, I'd forgotten I posted this to the forum. After getting a response from Apple on my feedback I changed the code to something like this:

struct ContentView: View {
    // values normally specified by the caller....
    let location = CLLocationCoordinate2D(latitude: 37.0, longitude: -122.0)
    let name = "name"

    @State private var position: MapCameraPosition = .automatic
    @State private var selectedId: String?

    var body: some View {
        let markers = makeMarkers(for: name, location: location)
        Map(position: $position, selection: $selectedId) {
            ForEach(markers) { marker in
                Marker(marker.title, coordinate: marker.location)
                    .tint(.red)
            }
        }
    }

    // Apparently needed for marker selection
    struct MarkerModel: Identifiable {
        var id: String = UUID().uuidString
        var location: CLLocationCoordinate2D
        var title: String
    }

    private func makeMarkers(for text: String, location: CLLocationCoordinate2D) -> [MarkerModel] {
        let marker = MarkerModel(location: location,
                                 title: text)
        return [marker]
    }
}

Code typed into this reply and may well contain typos. Anyway, using MKMapItem did not work. Not shown is the code that checks if a marker is selected where I put up a LookAround view. I do that in an overlay of the Map view.

Can you make a Marker selectable?
 
 
Q