MapKit, using snapper to populate map

Thanks very much for your help. I have made little progress and am getting very frustrated.

I have got the code to the stage as shown below.

I cannot work out how to get the onAppear() to fire and populate the markers.

Please can you help me further?

Thanks, Geoff

//
//  ContentView.swift
//  Chris Solution
//
//  Created by Geoff Smith on 28-01-2024
//

import SwiftUI
import MapKit

struct ContentView: View {
    @State var tags: [String] = []
    @State var lats: [Double] = []
    @State var lons: [Double] = []
    
    @State var latCount: Int = 0
    @State var longCount: Int = 0
    @State var tagCount: Int = 0
    
    @State var mytext: String = "Working on it"
    
    @State var camera: MapCameraPosition = .automatic
    
    
    var body: some View {
        VStack{
            Text(mytext)
                .onAppear() {
                    populateMarkers()
                    
                }
                
            }
        }
                        
    
    func populateMarkers() {
        lats.append(-25.757947887867694) //Blue Train
        lons.append(28.189196654067267)
        tags.append("bluetrain")
        
        lats.append(-17.82536423477478) //liv air
        lons.append(25.81926011162876)
        tags.append("livAir")
        
        lats.append(-26.13305962090586) //jo air
        lons.append(28.23189168563858)
        tags.append("jo_air")
        
        lats.append(-33.971204942427406)//cap air
        lons.append(18.598276359810573)
        tags.append("cap_air")
        
        lats.append(-26.14440600932509) // Peer
        lons.append(28.22131032404206)
        tags.append("Peer")
        
        lats.append(-17.864082081117978) //lvingstone train
        lons.append(25.854742557680495)
        tags.append("Liv Train")
        
        lats.append(-17.925316439275523)
        lons.append(25.858513402639517) // vic falls
        tags.append("V_Falls")
        
        lats.append(-25.185360010673655)
        lons.append(27.1470279923556) //ivy lodge
        tags.append("Ivy")
        
        lats.append(-25.24023368183164)
        lons.append(27.0759065323271) //Pilan
        tags.append("Pilan")
        
        latCount =  lats.count
        longCount = lons.count
        tagCount =  tags.count
    }
    
}
    
#Preview {
    ContentView()
}

Your code is fine.

If you want to see it working when running it n the Preview canvas then all you need to do is add a few print statements at the end of the function to show that populateMarkers has been called and did populate the arrays. You will see that data being printed in the debug panel at the bottom of the Xcode window. If you can’t see that panel then use the keyboard short cut Shift + Command + C to make it visible.

struct ContentView: View {
    @State var tags: [String] = []
    @State var lats: [Double] = []
    @State var lons: [Double] = []

    @State var latCount: Int = 0
    @State var longCount: Int = 0
    @State var tagCount: Int = 0

    @State var mytext: String = "Working on it"

    @State var camera: MapCameraPosition = .automatic


    var body: some View {
        VStack{
            Text(mytext)
                .onAppear() {
                    populateMarkers()
                }
        }
    }


    func populateMarkers() {
        lats.append(-25.757947887867694) //Blue Train
        lons.append(28.189196654067267)
        tags.append("bluetrain")

        lats.append(-17.82536423477478) //liv air
        lons.append(25.81926011162876)
        tags.append("livAir")

        lats.append(-26.13305962090586) //jo air
        lons.append(28.23189168563858)
        tags.append("jo_air")

        lats.append(-33.971204942427406)//cap air
        lons.append(18.598276359810573)
        tags.append("cap_air")

        lats.append(-26.14440600932509) // Peer
        lons.append(28.22131032404206)
        tags.append("Peer")

        lats.append(-17.864082081117978) //lvingstone train
        lons.append(25.854742557680495)
        tags.append("Liv Train")

        lats.append(-17.925316439275523)
        lons.append(25.858513402639517) // vic falls
        tags.append("V_Falls")

        lats.append(-25.185360010673655)
        lons.append(27.1470279923556) //ivy lodge
        tags.append("Ivy")

        lats.append(-25.24023368183164)
        lons.append(27.0759065323271) //Pilan
        tags.append("Pilan")

        latCount =  lats.count
        longCount = lons.count
        tagCount =  tags.count
        
        //  Print statements
        print("latCount: \(latCount)")
        print("longCount: \(longCount)")
        print("tagCount: \(tagCount)")
    }
}

You may recall that I suggested in a previous post (back in early January) that you create a struct to store each instance of a Map Marker. For example:

struct LandMark {
    var latitude: Double
    var longitude: Double
    var tag: String
}

Note: I suggested naming the struct MapMarker which in hindsight could have clashed with an existing SwiftUI Map protocol so call it something else.

and then declare an array of mapMarkers like this:

@State var mapMarkers = [LandMark]()

and then have a function to populate those markers like this:

    func populateMapMarkers() {
        mapMarkers.append(LandMark(latitude: -25.757947887867694, longitude: 28.189196654067267, tag: "bluetrain")) // Blue Train
        mapMarkers.append(LandMark(latitude: -17.82536423477478, longitude: 25.81926011162876, tag: "livAir")) // liv air
        // Add the remainder of the markers in a similar fashion
    }

and call that in your onAppear code:

        .onAppear {
            populateMapMarkers()
        }

Thank you very much for your help and support - would not have got this far with out your help and assistance.

I think I have implemented all your advice. I have nob got other problems.

When I launch the app it worked and text messages appear in the simulator and on my device. The app does not advance to displaying the actual map.

Please can you help?

Code below.

// Created by Chris Ching on 2023-07-13.

import SwiftUI
import MapKit

struct MapMarker {
var latitude: Double
var longitude: Double
var tag: String
}

struct ContentView: View {

@State var tags: [String] = []
@State var lats: [Double] = []
@State var lons: [Double] = []

@State var latCount: Int = 0
@State var longCount: Int = 0
@State var tagCount: Int = 0

@State var mytext: String = "Working on it"
@State var onap: String = "In on appear"

@State var camera: MapCameraPosition = .automatic


var body: some View {
    VStack{
        Text(onap)
            .onAppear {
                
                populateMapMarkers()
            }
    }
}

@State var mapMarkers = [MapMarker]()  // Create an array of map Markers



func populateMapMarkers() {
        
    mapMarkers.append(MapMarker(
        latitude: -25.757947887867694,
        longitude: 28.189196654067267,
        tag: "bluetrain"))            // Blue Train
        
    mapMarkers.append(MapMarker(
        latitude: -17.82536423477478,
        longitude: 25.81926011162876,
        tag: "livAir")) // liv air
        
    // Add the remainder of the markers in a similar fashion
    
    mapMarkers.append(MapMarker(
        latitude: -26.13305962090586,
        longitude: 28.23189168563858,
        tag: "JoAir")) // JoAir
    
    mapMarkers.append(MapMarker(
        latitude: -33.971204942427406,
        longitude: 18.598276359810573,
        tag: "CapAir")) // CapAir
    
    mapMarkers.append(MapMarker(
        latitude: -26.14440600932509,
        longitude: 8.22131032404206,
        tag: "Peer")) // Peer
    
    }

}

#Preview {
ContentView()
}