TomHa
TomHa

Reputation: 3

on iOS with mapkit "multible" how do display markers

Getting a single marker was easy, trying to click on main points an leave behind a marker seems easy but I am getting a weird error. Anybody been down this path?

Define array for markers

@State private var markers: [MarkerData] = []

Inside Mapview class

var body: some View {
    VStack(){
        
        GeometryReader { geometryProxy in
            MapReader { mapProxy in
                Map(position: $cameraPosition, interactionModes: modes) {
                    if let markerData {
                        Marker("Spot", coordinate: markerData.coordinate)
                    }
// This does not work.
// error: Type '()' cannot conform to 'MapContent'
                    markers.forEach { marker in
                        print(marker.coordinate)
                    }
                }
                .onTapGesture { screenCoordinate in
                    self.markerData = mapProxy.markerData(screenCoordinate: screenCoordinate, geometryProxy: geometryProxy)
                    
                    if let markerData = self.markerData{
                        markers.append(markerData)
                    }
                }

The line “//error” shows the error I'm getting and the on onTapGesture shows how I add them to the list

The sample source code can be found on GitHub https://github.com/tomha2014/LearnMapKitiOS17

Upvotes: 0

Views: 334

Answers (0)

Related Questions